2013-02-20 52 views
0

我想将外部类添加到不在我们域上的链接...容易找到解决方案,但它不捕获子域。将外部类添加到不在域或子域上的链接

那么,如何在只有href主机位置不包含'hw.ac.uk'的标签上使用addClass(“external”)?

以下是流行,但不赶子域(其中我们可以是一个或两个深,例如,子域,www.subdomain。等)

$("a").filter(function() { 
return this.hostname && this.hostname !== location.hostname; 
}).addClass('external'); 

我想我想的相反的add class to all links that link to a certain domain with js (jquery)

感谢

尼尔

回答

-1
// Creating custom :external selector 
$.expr[':'].external = function(obj){ 
    return !obj.href.match(/^mailto\:/) 
      && (obj.hostname != location.hostname); 
}; 

// Add 'external' CSS class to all external links 
$('a:external').addClass('external'); 

编辑 没有捕捉到子域问题。 Tha上面的代码只考虑顶级域名

+0

对不起,没有赶上子域问题,当我第一次阅读! – oleron 2013-02-20 12:09:57