简体   繁体   English

在ul的每个第一个孩子上添加类,并在每个悬停时将其删除

[英]add class on each first-child of ul and remove on hover on each

I have a ul repeat as a different category. 我有一个ul重复作为另一个类别。 With the help of jquery i added the class on every first child of ul li, then i removed it whenever i hover on any li. 借助于jquery,我在ul li的每个第一个孩子上添加了该类,然后每当我将鼠标悬停在任何li上时就将其删除。 But one of the defect is Whenever i hover on any element every category ul li first-child classes removed but I want only specefic ul li first-child class to be removed when hover at a time. 但是缺点之一是,只要我将鼠标悬停在任何元素上,则删除所有类别的第一个孩子类,但我只希望一次悬停时删除特定的ul第一孩子类。

$(".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 html

<div class="product-left">



     <ul>
              <li><div class="pr_name"> <a href=" subproductpage.php?id=4"><div class="img_50_35">
        <img width="50" height="35" src="childsubcategory_image/20120925_085744_accomodation.gif"> </div>
        <span class="product_nm"> cosmo_sub_child_2 </span></a>
        </div>
  </li>
                 <li><div class="pr_name"> <a href=" subproductpage.php?id=3"><div class="img_50_35">
        <img width="50" height="35" src="childsubcategory_image/20120925_080004_search.gif"> </div>
        <span class="product_nm"> cosmo_sub_child </span></a>
        </div>
       </li>
                 </ul>




        </div>

Use this within the mouseover event handler to refer to the element for which the event was triggered: mouseover事件处理程序中使用this函数可以引用为其触发事件的元素:

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

try this 尝试这个

$(".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");
});​

FIDDLE with a FIDDLE

tag instead if tag 标记,如果标记

UPDATED CODE 更新的代码

UPDATED FIDDLE 更新场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM