jquery最接近特定div内的图像

时间:2017-02-16 09:19:16

标签: php jquery html

我在页面中使用了以下HTML

<div class="edge-img-sec">
    <img src="img1" alt="edge-icon" class="edge-icon1">
    <img src="img2" alt="right-icon" class="edge-icon1-right">
</div>

<div class="edge-img-sec">
    <img src="img3" alt="edge-icon" class="edge-icon1">
    <img src="img4" alt="right-icon" class="edge-icon1-right">
</div>

Jquery脚本: -

$( ".edge-img-sec" ).click(function(){
    alert('aa');
    $(this).closest('img').find('.edge-icon1-right').hide();
});

点击edge-img-sec div我要隐藏相应的edge-icon1-right图像。

我使用了上面的代码。但它没有用。我在这做错了什么。请帮我。

2 个答案:

答案 0 :(得分:4)

您可以像下面这样(使用children()): -

&#13;
&#13;
$( ".edge-img-sec" ).click(function(){
    $(this).children('.edge-icon1-right').hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="edge-img-sec">
    <img src="img1" alt="edge-icon" class="edge-icon1"><br>
    <img src="img2" alt="right-icon" class="edge-icon1-right"><br>
</div>
<br>
<div class="edge-img-sec">
    <img src="img3" alt="edge-icon" class="edge-icon1"><br>
    <img src="img4" alt="right-icon" class="edge-icon1-right"><br>
</div>
&#13;
&#13;
&#13;

或使用find(): -

&#13;
&#13;
$( ".edge-img-sec" ).click(function(){
    $(this).find('.edge-icon1-right').hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="edge-img-sec">
    <img src="img1" alt="edge-icon" class="edge-icon1"><br>
    <img src="img2" alt="right-icon" class="edge-icon1-right"><br>
</div>
<br>
<div class="edge-img-sec">
    <img src="img3" alt="edge-icon" class="edge-icon1"><br>
    <img src="img4" alt="right-icon" class="edge-icon1-right"><br>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

试试这个:

  $( ".edge-img-sec" ).click(function(){
        $(this).find('img.edge-icon1-right').hide();
  });