2011-02-12 97 views
0

下面的jQuery腳本在我的網站創建與其他Java/MooTools的腳本的兼容性問題 - 有什麼我可以做,使之更加兼容jQuery腳本+兼容性

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

注 - 該腳本旨在在新窗口中打開外部鏈接。

回答

2

是的,要麼將$全部替換爲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> 

...因爲第一個參數是匿名函數是jQuery的命名空間。在這裏我已將它重新分配給$內部。

預期它應該工作。

0

闡述你的意思是由兼容性問題是什麼。

這是可能的,你可能需要使用jQuery.noConflict(),在與其他庫一起使用jQuery如MooTools的時尤其如此。

但是,既然你已經有了MooTools的,爲什麼不使用它呢?