Headline说明了一切。我试图实现的是检查菜单中的onload ul (多个ul,因为它也有子菜单),如果等于,则将其设置为粗体并触发mouseover事件:
$(document).ready(function(){
$('#content > ul > li a[href]').filter(function() {return this.href.pathname === window.location.pathname;}).css('font-weight','bold');
$('#content > ul > li a[href]').filter(function() {return this.href.pathname === window.location.pathname;}).trigger('mouseover');
});
<ul id="menu">
<li><a href="#" onmouseover="displayID('biography');">Biography</a></li>
<li><a href="/tagged/Commercial_photography" onmouseover="displayID('commercial-photography');">Commercial photography</a></li>
<li><a href="/tagged/Fine_art_photography" onmouseover="displayID('fine-art-photography');">Fine art photography</a></li>
<li><a href="/tagged/Action" onmouseover="displayID('action');">Action</a></li>
<li><a href="/tagged/Video" onmouseover="displayID('video');">Video</a></li>
<li><a href="#" onmouseover="displayID('links');">Links</a></li>
</ul>我想这只是一个非常简单的错误,因为我对jquery并不是很熟悉,所以我非常感谢大家的帮助
发布于 2011-11-01 17:18:54
链接的href属性是一个没有pathname属性的字符串。
使用this.pathname而不是this.href.pathname来解决您的问题:
function(){
return this.pathname === location.pathname &&
!/^#/.test(this.getAttribute('href')) //The href at HTML may not
} //start with #注释后编辑:
getAttribute方法用于获取原始的href属性,因为this.href不包含#,而包含http://fullpath/etc/file#。
https://stackoverflow.com/questions/7964461
复制相似问题