2011-02-11 79 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')} 
);