Onclick on image I need to change the background of li using jquery
LI is not immediate parent so I am not able to use parent() function in jquery
<li class="ui-widget-content ui-corner-开发者_StackOverflowtr ui-draggable">
<fb:profile-pic height="32" width="32" linked="false" uid="557103888" style="width: 32px; height: 32px;" class=" fb_profile_pic_rendered">
<img class="" style="width: 32px; height: 32px;" title="some name" alt="some name" src="files/t557103228_5135.jpg">
</fb:profile-pic>
</li>
Use .closest('li')
.
This will give you the first ancestor of the <img>
that is an <li>
element.
You could also use .parents('li:first')
. It uses the :first
selector to ensure you only get the first result.
You can do like this:
$('img[title="some name"]').click(function(){
$(this).closest('li.ui-widget-content').css('background', 'green');
});
Since the image does not have any class
or id
attribute set, the title
attribute is used instead. In this case any image whose title is set to some title
for example gets clicked, the background of parent li
with class ui-widget-content
is changed using css
method which is actually found with closest
function which finds elements back up until specified element is found.
It's the grandparent, so .parent().parent()
then.
Or use .closest('li')
.
精彩评论