开发者

JQuery add class to parent element

开发者 https://www.devze.com 2023-01-12 01:24 出处:网络
I have to set a class name on a (li) element. This script find all the (a) elements in the list, and creates a click event.

I have to set a class name on a (li) element. This script find all the (a) elements in the list, and creates a click event.

jQuery("#" + ElementID).find(TagName).click(function () {

    GetPageByUrl(jQuery(this).attr("href"));
    jQuery(this).parent().addClass('yourClass');
    //ChangeSelectedMenuItem(this);
    return false;
});

The parent of every (a) element is a (li) 开发者_StackOverflow社区element

But nothing happens when this line is executing jQuery(this).parent().addClass('yourClass');

Everything else is working just fine.

What am I doing wrong here?


Okay, but it still won't work. It won't add any class jQuery(this).addClass('yourClass'); Should add a class to the (a) element, but it doesn't?


Specify the optional selector to target what you want:

jQuery(this).parent('li').addClass('yourClass');

Or:

jQuery(this).parents('li').addClass('yourClass');


$(this.parentNode).addClass('newClass');
0

精彩评论

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