2011-02-11 70 views
0

我有一些jQuery代碼可以在新窗口中打開外部鏈接,雖然我遇到了與Joomla站點和jQuery的兼容性問題。過去我遇到過這個問題,解決它的最簡單方法是使用Joomla系統Mootools庫。將jQuery轉換爲Mootools

這裏的jQuery腳本我需要轉換成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> 

可能有人請幫我轉換上面的代碼?

再次,Mootools已通過系統插件啓用。

+1

您可以使用更好的命名空間。 – Yahel 2011-02-11 23:39:34

+0

查看[jquery api](http://api.jquery.com/)中的`noconflict`。在jQuery中聲明`document.ready`事件的最好方法是這樣的:`jQuery(function($){...您的代碼在這裏...});`因爲它將jQuery替換爲`$`功能的上下文。 – zzzzBov 2011-02-11 23:47:34

+0

因此,此代碼將target =「_ blank」添加到所有錨鏈接標籤,其中的href屬性包含的URL與當前正在提供的網頁的主機名不同?或tl; dr:使外部網站在新窗口中打開。對? – 2011-02-11 23:47:40

回答

3

如果你可能要選擇外部鏈接再次,你可以建立一個新的僞選擇

Slick.definePseudo('external', function() { 
    return this.hostname && this.hostname != window.location.hostname; 
}); 

document.getElements('a[href^=http://]:external').set('target', '_blank'); 

或者正是因爲jQuery的做到了。

document.getElements('a[href^=http://]').filter(function(a) { 
    return a.hostname && a.hostname != window.location.hostname 
}).set('target', '_blank'); 
0
$('a').each(function(el){ 
    /* check el.href and if test is true => */ 
    el.set('target','_blank')} 
);