Is it possible to select $(this) AND use selectors in jQuery

…衆ロ難τιáo~ 提交于 2019-12-21 07:28:25

问题


I'm wondering if I can use $(this) as well as a class selector before running a function on them.

So rather than doing;

$(this).toggleClass('open');
$('.closed').toggleClass('open');

Do something more like;

$(this, '.closed').toggleClass('open');

Whereas really, the above will select 'this' within the context of '.closed'

Regards,


回答1:


You can use add():

$(".closed").add(this).toggleClass("open");

It will add this element to the set of matched elements (i.e. .closed).



来源:https://stackoverflow.com/questions/12582956/is-it-possible-to-select-this-and-use-selectors-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!