Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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仅将类添加到子元素_Jquery - Fatal编程技术网

jQuery仅将类添加到子元素

jQuery仅将类添加到子元素,jquery,Jquery,以下代码起作用: jQuery(".left-content").find("li").hover(function () { jQuery(".left-content").find(".stamp").addClass('stamp-hover'); }, function () { jQuery(".left-content").find(".stamp").removeClass('stamp-hover'); } ); 但是,我在页面上以相同的方式设置了

以下代码起作用:

jQuery(".left-content").find("li").hover(function () {
    jQuery(".left-content").find(".stamp").addClass('stamp-hover');
  }, 
  function () {
    jQuery(".left-content").find(".stamp").removeClass('stamp-hover');
  }
);
但是,我在页面上以相同的方式设置了大约5个div,当将鼠标悬停在li上(比如div 1)时,该类将添加到页面上
div.left-content
中的所有
div.stamp
。其中,我只希望将类添加到父级
div.left-content
中的
div.stamp

<div class="left-content">
    <div class="content-section">
        <ul>
            <li><a href="#">link 1</a></li>
            <li><a href="#">link 2</a></li>
            <li><a href="#">link 3</a></li>
        </ul>
    </div>
    <div class="stamp-anchor-wrapper">
        <div class="stamp"></div>
    </div>
</div>


使用

jQuery(".left-content").find("li").hover(function () {
    jQuery(this).parents(".left-content").find(".stamp").addClass('stamp-hover');
  }, 
  function () {
    jQuery(this).parents(".left-content").find(".stamp").removeClass('stamp-hover');
  });

使用
this

jQuery(".left-content").find("li").hover(function () {
    jQuery(this).parents(".left-content").find(".stamp").addClass('stamp-hover');
  }, 
  function () {
    jQuery(this).parents(".left-content").find(".stamp").removeClass('stamp-hover');
  });