是否可以选择$(this)并在jQuery中使用选择器 [英] Is it possible to select $(this) AND use selectors in jQuery

查看:69
本文介绍了是否可以选择$(this)并在jQuery中使用选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在运行函数之前我是否可以使用$(this)以及类选择器。

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

所以不要这样做;

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

做更多的事情;

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

实际上,上面将在'.closed'的上下文中选择'this'

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

问候,

推荐答案

你可以使用 add()

You can use add():

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

它会将元素添加到集合中匹配元素(即<$​​ c $ c> .closed )。

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

这篇关于是否可以选择$(this)并在jQuery中使用选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆