下面的jQuery脚本在我的站点上创建了与其他java/mootools脚本的兼容性问题--有什么可以让它更兼容的吗?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a[href^="http://"]').filter(function() {return this.hostname && this.hostname !== location.hostname;}).attr('target', '_blank');
});
</script>注意-脚本被设计为在新窗口中打开外部链接。
发布于 2011-02-12 00:19:28
是的,要么将所有$替换为jQuery,要么使用jQuery的noConflict()模式,它不将jQuery分配给$。
或者你可以这么做..。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[href^="http://"]').filter(function() {return this.hostname && this.hostname !== location.hostname;}).attr('target', '_blank');
});
</script>...because该匿名函数的第一个参数是jQuery命名空间。在这里,我在内部将它重新分配给了$。
它应该能像预期的那样起作用。
发布于 2011-02-12 00:20:44
详细说明您所说的兼容性问题是什么意思。
您可能需要使用jQuery.noConflict(),特别是在与其他库(如MooTools )一起使用jQuery时。
但是,既然您已经拥有了MooTools,为什么不直接使用它呢?
https://stackoverflow.com/questions/4975362
复制相似问题