定位元素

时间:2017-09-08 14:01:54

标签: jquery css targeting

点击后,我想定位父母的父母兄弟的孩子。将有多个容器。因此,当单击删除时,我希望仅更改该div中的img。

这是我现在的jquery - 没有运气我错过了额外的父母吗?



$('.removal').click(function() {
  $('this').parent().siblings('.item-img').find('.productImage').addClass('cart-removal-item-img')
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item-img">
    <img class="productImg" src... />
  </div>

  <div class="rndm1">
  </div>

  <div class="rndm1">
  </div>

  <div class="item-action">
    <div class="rndm1"></div>
    <p><a class="removal" href="">remove</a></p>
  </div>

</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

当我修复两个拼写错误(引用&#39;这个&#39;和拼写错误的图像类)时,我得到了这段代码

&#13;
&#13;
$('.removal').on("click",function(e) {
  e.preventDefault(); // cancel the click
  $(this).closest(".container") // the container div
    .find('.productImg') // the image class in question
    .addClass('cart-removal-item-img');
});
&#13;
.cart-removal-item-img { display: none }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item-img">
    <img class="productImg" src="http://lorempixel.com/400/200/sports/" />
  </div>

  <div class="rndm1">
  </div>

  <div class="rndm1">
  </div>

  <div class="item-action">
    <div class="rndm1"></div>
    <p><a class="removal" href="">remove</a></p>
  </div>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我确实回答了我自己的问题,添加了一个额外的.parent()

<script>
 $('.removal').click(function(){
        $(this).parent().parent().siblings('.item-img').find('.productImage').addClass('cart-removal-item-img')
    });
</script>