Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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 正在尝试将活动类添加到菜单项_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 正在尝试将活动类添加到菜单项

Javascript 正在尝试将活动类添加到菜单项,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我花了2个小时试图创建一个活动菜单,但没有成功,所以我想我会寻求帮助。我有一个html菜单,一些js和css $(函数(){ var pgurl=window.location.href.substr(window.location.href .lastIndexOf(“/”+1); $(“#索引li a”)。每个(函数(){ if($(this.attr(“href”)==pgurl | |$(this.attr(“href”)=='') $(此).addClass(“活动”); }) })

我花了2个小时试图创建一个活动菜单,但没有成功,所以我想我会寻求帮助。我有一个html菜单,一些js和css

$(函数(){
var pgurl=window.location.href.substr(window.location.href
.lastIndexOf(“/”+1);
$(“#索引li a”)。每个(函数(){
if($(this.attr(“href”)==pgurl | |$(this.attr(“href”)=='')
$(此).addClass(“活动”);
})
});
.active{font-weight:bold;}


我不清楚您的问题,但您是否希望将active class设置为menu,因此请尝试下面的代码

$(“#导航列表a”)。单击(函数(e){
e、 preventDefault();//防止链接被跟踪
$(“#导航列表a”).removeClass('selected');
$(this.addClass('selected');
});
.nav{
颜色:绿色;
}
.选定{
颜色:红色;
}
李先生{
浮动:左;
右边距:25px;
}


尝试以下操作并检查其是否有效

$(function() {
    var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
    $("#index li a").each(function() {
        var href = "";
        if($(this).attr("href") != '')
            href = $(this).attr("href").substr(($(this).attr("href").lastIndexOf("/")+1);
            if (href == pgurl || href  == '')
                  $(this).addClass("active");
      })
  });

此行获取页面名称:

var pgurl = window.location.href.substr(window.location.href
                  .lastIndexOf("/") + 1);
您需要对每个链接应用相同的逻辑,以查看它们是否匹配:

$("#index li a").each(function() {

  var aurl = $(this).attr("href").substr($(this).attr("href")
                    .lastIndexOf("/")+1);

  if (aurl == pgurl || aurl == '')
    $(this).addClass("active");
})

下面的代码段(调整为匹配为location.href不匹配)

$(函数(){
//var pgurl=window.location.href.substr(window.location.href
//.lastIndexOf(“/”+1);
var locationhref=“mysite.com/country the double/introduction”
var pgurl=locationhref.substr(locationhref.lastIndexOf(“/”)+1);
$(“#索引li a”)。每个(函数(){
var aurl=$(this.attr(“href”).substr($(this.attr(“href”)
.lastIndexOf(“/”+1);
//console.log(aurl)
如果(aurl==pgurl | | aurl=='')
$(此).addClass(“活动”);
})
});
.active{字体大小:粗体;颜色:黄色;}


只需遍历整个链接,当单击任何链接时,将字体权重:正常更改为所有链接,然后将字体权重:粗体应用于当前单击的链接

$().ready(function () {
        $("ul>li").click(function () {
            $("ul>li").css("font-weight", "normal");
            $(this).css("font-weight", "bold");
        });
    });

如果您发现类似的值,您是否尝试提醒pgurl和href单击“简介”部分时,您的
pgurl
变量将存储
introduction
,但是您的
href
中进行检查时,每个
循环都将
。/与赔率/简介
相反。值得思考。