如果页面 url = 列表中的锚点,则触发鼠标悬停 +大胆

发布于 2024-12-12 18:12:55 字数 1225 浏览 5 评论 0原文

标题说明了一切。我想要实现的是检查菜单中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-12-19 18:12:55

链接的 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#

The href property of a link is a string, which has no pathname property.

Use this.pathname, instead of this.href.pathname to solve your issue:

function(){
    return this.pathname === location.pathname &&
           !/^#/.test(this.getAttribute('href'))   //The href at HTML may not 
}                                                 //start with #

Edit after comment:
The getAttribute method is used to get the original href attribute, because this.href does not contain #, but http://fullpath/etc/file#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文