2011-05-17 96 views
2

我正在处理一个项目,我想突出显示与站点上其他链接具有相同href链接(在菜单中)(在.container中) 。jQuery:突出显示与另一个链接具有相同href的链接

<ul class="menu"> 
    <li><a href="about.html">Link 1</a></li> 
    <li><a href="portfolio.html">Link 2</a></li> 
    <li><a href="contact.html">Link 3</a></li> 
</ul> 
<div class="container"> 
    <a href="contact.html">Go to Contact</a> 
</div> 

JS:

$("a").filter(function() { 
    return this.href === $('.container a').href; 
}).addClass("equalHref"); 

你知道我怎么能做到这一点?

回答

0

你的解决方案是非常接近正确的:

$("a").filter(function() { 
    // use jQuery.attr to access href 
    return this.href === $('.container a').attr("href"); 
}).addClass("equalHref"); 

或:

$("a").filter(function() { 
    // expose DOM object and access href property 
    return this.href === $('.container a')[0].href; 
}).addClass("equalHref"); 
1
$('a:[href="' + $('.container a').attr("href") + '"]').addClass("equalHref"); 

测试here

+0

我总是迟到5秒! – 2011-05-17 20:43:27

0
var strHref = $('.container a').attr("href");  
$("a[href=" + strHref + "]")addClass("equalHref")