Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 jQuery和活动菜单项-什么是最好的?_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery和活动菜单项-什么是最好的?

Javascript jQuery和活动菜单项-什么是最好的?,javascript,jquery,html,Javascript,Jquery,Html,我正在制作一个菜单,我想将class=“selected”添加到活动菜单项中。当URL类似于以下内容时,我对location.pathname有问题: - 因此,它基本上只在url如此简单时才起作用: - 我知道有很多类似的问题,但如果你看一下下面的代码,我使用的是一种“新”方法,但它并没有真正起作用,这就是你们跳进去的地方:) Javascript代码: var path = location.pathname; $("a[href='" +

我正在制作一个菜单,我想将class=“selected”添加到活动菜单项中。当URL类似于以下内容时,我对
location.pathname
有问题: -

因此,它基本上只在url如此简单时才起作用: -

我知道有很多类似的问题,但如果你看一下下面的代码,我使用的是一种“新”方法,但它并没有真正起作用,这就是你们跳进去的地方:)

Javascript代码:

            var path = location.pathname;

            $("a[href='" + [path] + "']").parents("li").each(function() {   
                    $(this).addClass("selected");
            });
有什么想法可以让jQuery实现这一点吗

如果有人认为我的方法不好或不理想,请告诉我为什么,并张贴或链接我您的解决方案


提前谢谢

组合location.pathname和location.search创建URL片段

为什么不这样做:

// Check complete URL
var path = location.href;
$("#navID li a").each(function() {
    // Check if there is a match between the URL and the navigation link    
    if(path.match($(this).attr("href")) $(this).parent().addClass("selected");
});

因为在我的hrefs中,我没有完整的URL:S basicali=>href=“”无法匹配href=“adrov48.php”…您实际上是在这里将“adrov48.php”匹配到“google.com/sub folder/adrov48.php”。它在这里工作:;我已经添加了一些评论来解释为什么它不能在你的小提琴上工作。谢谢你的努力,一切正常,但还有一个问题,那就是如果URL是
http://google.com/sub-folder/adrov48/announcements.php?id=204
。。。在这里拉小提琴=>