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,我的布局如下: <ul id="header"> <li id="item1" class="off"> <a>Abc</a> </li> <li id="item2" class="off"> <a>Abc</a> </li> </ul> 有什么想法吗 --编辑: 好吧,我刚刚意识到我没有正确地看待这个问题 因此,当点击该链接时,将加载一

我的布局如下:

<ul id="header">
     <li id="item1" class="off">   <a>Abc</a>   </li>
     <li id="item2" class="off">   <a>Abc</a>   </li>
</ul>
有什么想法吗

--编辑: 好吧,我刚刚意识到我没有正确地看待这个问题

因此,当点击该链接时,将加载一个新页面。所以使用click函数是错误的,因为加载了一个新页面,所以对类所做的任何更改都将丢失。因此,我需要使用
$(document).ready(function()
)来表示类似“我在上一页中用id单击了li,所以现在更新该类”

所以

谢谢!

您可以使用获取父级
li
,然后添加类:

$(this).closest("li").addClass("on");
您还需要使用id选择器
$(“#header”)
而不是类选择器
$(“.header”)

使用

$(".header > li a").click(function(){
      $(".on").removeClass("on");
      $(this).closest("li").addClass("on");


});
工作小提琴

它的事件链

看,李不是学生。


你的
选择器错误
$(“.header”)。在你的html代码
中,你必须使用
id
选择器
$(“#header”)


+1旁注:在英语中,句子以大写字母开头。:-)当我点击链接时,什么也没发生。是“$”(“.header>li a”)。单击(函数(){def.correct??
$("#header > li a").click(function(){

      $("#header li a.current").removeClass("off");
        $(this).closest("li").addClass("on");

});
$(".header > li a").click(function(){
      $(".on").removeClass("on");
      $(this).closest("li").addClass("on");


});
$("#header > li a").click(function(){    
      $(this).parent().removeClass("off").addClass("on");          
});
$("#header li a").click(function (e) {
    e.preventDefault();
    $(this).parent().siblings().removeClass("on").addClass("off"); // Remove all class on and added off class
    $(this).parent().removeClass("off").addClass("on"); // Select current parent element and remove off and added on class


});