Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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
Jquery 在ul的每个第一个子级上添加类,并在每个子级上悬停时删除_Jquery - Fatal编程技术网

Jquery 在ul的每个第一个子级上添加类,并在每个子级上悬停时删除

Jquery 在ul的每个第一个子级上添加类,并在每个子级上悬停时删除,jquery,Jquery,我有一个ul重复作为一个不同的类别。在jquery的帮助下,我在ulli的每个第一个子类上添加了这个类,然后每当我在任何li上悬停时,我都会删除它。但其中一个缺陷是,每当我将鼠标悬停在任何元素上时,每个类别ul li第一个子类都会被删除,但每次悬停时,我只希望specefic ul li第一个子类被删除 $(".product-left ul li:first-child img").addClass("first_child"); $(".product-left ul li ").mous

我有一个ul重复作为一个不同的类别。在jquery的帮助下,我在ulli的每个第一个子类上添加了这个类,然后每当我在任何li上悬停时,我都会删除它。但其中一个缺陷是,每当我将鼠标悬停在任何元素上时,每个类别ul li第一个子类都会被删除,但每次悬停时,我只希望specefic ul li第一个子类被删除

$(".product-left ul li:first-child img").addClass("first_child");

$(".product-left ul li ").mouseover(function() {
  $(".product-left ul li:first-child img" ).each.removeClass("first_child");
}).mouseout(function() { 
  $(".product-left ul li:first-child img").addClass("first_child");
});
html



鼠标盖
事件处理程序中使用
,以引用触发事件的元素:

$(".product-left ul li").mouseover(function() {
    $("img", this).removeClass("first_child");
}).mouseout(function() { 
    $("img", this).addClass("first_child");
});
试试这个

$(".product-left ul").find('img:eq(0)').addClass("first_child");
$(".product-left ul li").mouseover(function() {
    $(this).parent().find("img:eq(0)").removeClass("first_child");
}).mouseout(function() {
    $(this).parent().find("img:eq(0)").addClass("first_child");
});​
用标记代替if标记

更新代码


很抱歉,但是代码没有删除第一个子类您也可以使用$(this).find('img').get();它没有在第一个孩子的img中添加任何类:(你检查过小提琴了吗?…这不是你想要的方式吗..当鼠标悬停在特定的ul上时移除类,并将类重新添加到鼠标上我很抱歉我的div格式不清楚。我已经在我的帖子中更新了它。是的,我看到你摆弄它工作得很好,我无法在我的div中使用它:(您是否首先指定了第一个子类…?请检查上面更新的小提琴。)
$(".product-left ul").find('img:eq(0)').addClass("first_child");
$(".product-left ul li").mouseover(function() {
    $(this).parent().find("img:eq(0)").removeClass("first_child");
}).mouseout(function() {
    $(this).parent().find("img:eq(0)").addClass("first_child");
});​