jQuery $(this) 语法问题

标签 jquery this

这是一个有效的选择器吗?如果不是,正确的方法是什么?

$($(this)+' childElement')....

最佳答案

这可能就是您正在寻找的:

$('childElement', $(this))

基本上,它会在当前元素 this 的上下文中搜索 childElement

<小时/>

有关详细信息,请参阅 jQuery() 的文档功能。特别是,以下摘录解释了第二个参数以及它如何等效于使用 find:

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:

$('div.foo').click(function() {
  $('span', this).addClass('bar');
});

Since we've restricted the span selector to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').

关于jQuery $(this) 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922285/

相关文章:

jquery - GMail API 分页使用 nextPageToken

javascript switch 语句,如果字符串包含子字符串

javascript - jquery动态添加html字段遇到神秘问题

javascript - 为什么调用带有括号的方法,例如。 (obj.func)(),仍然设置 `this` 吗?

c++ - 通过 lambda 调用成员函数时意外未初始化的数据

c++ - 类之间的交叉引用和传递 This 指针 [NS2/C++]

jquery - 图像上的CSS文本宽度

javascript - 如何使用 jquery 为文档中的所有 iframe 添加 onclick 事件?

javascript - 使用 jQuery 显示嵌套的 UL

javascript - 如何在回调中访问正确的“this”?