首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果页面url =列表中的锚点,则触发鼠标悬停+加粗

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

Stack Overflow用户
提问于 2011-11-01 17:16:39
回答 1查看 277关注 0票数 3

Headline说明了一切。我试图实现的是检查菜单中的onload ul (多个ul,因为它也有子菜单),如果等于,则将其设置为粗体并触发mouseover事件:

代码语言:javascript
复制
$(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并不是很熟悉,所以我非常感谢大家的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-01 17:18:54

链接的href属性是一个没有pathname属性的字符串。

使用this.pathname而不是this.href.pathname来解决您的问题:

代码语言:javascript
复制
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#

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7964461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档