Javascript 如果页面url=列表中的锚点,则触发鼠标悬停+;大胆
标题说明了一切。我试图实现的是检查菜单中的onload URL(多个ul,因为它也有子菜单),如果等于,则将其设为粗体并触发鼠标悬停事件:Javascript 如果页面url=列表中的锚点,则触发鼠标悬停+;大胆,javascript,jquery,onload,html-lists,Javascript,Jquery,Onload,Html Lists,标题说明了一切。我试图实现的是检查菜单中的onload URL(多个ul,因为它也有子菜单),如果等于,则将其设为粗体并触发鼠标悬停事件: $(document).ready(function(){ $('#content > ul > li a[href]').filter(function() {return this.href.pathname === window.location.pathname;}).css('font-weight','bold');
$(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>
$(文档).ready(函数(){
$('#content>ul>li a[href]').filter(函数(){返回this.href.pathname===window.location.pathname;}).css('font-weight','bold');
$('#content>ul>li a[href]').filter(函数(){返回this.href.pathname===window.location.pathname;}).trigger('mouseover');
});
我想这只是一个非常简单的错误,因为我对jquery不是很熟悉,所以我非常感谢您的帮助链接的
href
属性是一个字符串,它没有路径名
属性
使用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#
当然,我错了!非常感谢。我注意到还有一个问题,它还使与#的链接变为粗体,例如。有没有办法排除这个?这太棒了!仅供将来参考,现在它只选择带有#的链接,但可以很容易地通过否定来解决:)谢谢Rob!