如果页面 url = 列表中的锚点,则触发鼠标悬停 +大胆
标题说明了一切。我想要实现的是检查菜单中的 onload url(多个 ul,因为它也有子菜单),如果相等,则将其设为粗体并触发鼠标悬停事件:
$(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,所以我非常感谢任何帮助
Headline says it all. What i am trying to achieve is to check onload urls in menu (multiple ul since it also has submenus) and if it equals, make it bold and trigger mouseover event:
$(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>
I assume there is just a very simple mistake since I am not really familiar with jquery, so I really appreciate any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接的
href
属性是一个字符串,没有pathname
属性。使用
this.pathname
而不是this.href.pathname
来解决您的问题:在评论后编辑:
getAttribute
方法用于获取原始href
属性,因为this.href
不包含#
,而是http://fullpath/etc/file#
。The
href
property of a link is a string, which has nopathname
property.Use
this.pathname
, instead ofthis.href.pathname
to solve your issue:Edit after comment:
The
getAttribute
method is used to get the originalhref
attribute, becausethis.href
does not contain#
, buthttp://fullpath/etc/file#
.