开发者

Onclick on image I need to change the background of li using jquery

开发者 https://www.devze.com 2023-01-13 08:06 出处:网络
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

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').

0

精彩评论

暂无评论...
验证码 换一张
取 消