开发者

jQuery script + compatibility

开发者 https://www.devze.com 2023-02-10 05:28 出处:网络
The following jQuery script creates compatibility issues with other java/mootools script on my site - is there anything I can do to make it more compatible?

The following jQuery script creates compatibility issues with other java/mootools script on my site - is there anything I can do to make it more compatible?

<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>

Note - the script is designed to open external links in new windows.


Yes, either replace all $ with jQuery, or use jQuery's noConflict() mode which doesn't assign jQuery to $.

Or you could do this...

<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 the first argument to that anonymous function is the jQuery namespace. Here I have reassigned it to $ internally.

It should work as expected.


Elaborate on what you mean by compatibility issues.

It's possible you may need to use jQuery.noConflict(), especially when using jQuery in conjunction with other libraries such as MooTools.

However, since you already have MooTools, why not just use it instead?

0

精彩评论

暂无评论...
验证码 换一张
取 消