Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果页面url=列表中的锚点,则触发鼠标悬停+;大胆_Javascript_Jquery_Onload_Html Lists - Fatal编程技术网

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

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');

标题说明了一切。我试图实现的是检查菜单中的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>
$(文档).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!