我有这个列表,我想在点击的标签上更改图像。我怎么能这样做?
<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>
答案 0 :(得分:2)
使用.siblings()
:
$('.Root a').click(function () {
var $img = $(this).siblings('img');
$img.attr('src', 'newimage.png')
});
$('.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;