在A标签上查找最近的图像单击

时间:2015-11-04 18:20:05

标签: javascript jquery html

我有这个列表,我想在点击的标签上更改图像。我怎么能这样做?

 <ul>
        <li class='Root'>
           <img src='icon_plus.png' /><input type='checkbox' /><a href='#'><span>sth</span></a>
        </li>
        <li class='Root'>
          <img src='icon_plus.png' /><input type='checkbox' /><a href='#'><span>sth</span></a>
        </li>
 </ul>
 <script>
     $('.Root a').click(function () {
             //How Can I do that????
     });
</script>

1 个答案:

答案 0 :(得分:2)

使用.siblings()

$('.Root a').click(function () {
    var $img = $(this).siblings('img');
    $img.attr('src', 'newimage.png')
});

&#13;
&#13;
    $('.Root a').click(function() {
      var $img = $(this).siblings('img');
      $img.attr('src', 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li class='Root'>
    <img src='icon_plus.png' />
    <input type='checkbox' /><a href='#'><span>sth</span></a>
  </li>
  <li class='Root'>
    <img src='icon_pluss.png' />
    <input type='checkbox' /><a href='#'><span>sth</span></a>
  </li>
</ul>
&#13;
&#13;
&#13;