这在jquery $('#id',javascript_object);中意味着什么? [英] What does this mean in jquery $('#id', javascript_object);
问题描述
这个$('#id', javascript_object);
在jquery中是什么意思.我知道我们可以将第二个参数中的上下文传递给选择器.但是当第二个参数是javascript对象时会发生什么.
what does this $('#id', javascript_object);
mean in jquery . I know that we can pass a context in the second parameter to the selector . But what happens when the second parameter is a javascript object .
预先感谢您的帮助.
推荐答案
第二个参数是选择器上下文.它将搜索限制为特定文档对象或特定DOM元素的子代.
The second parameter is the selector context. It restricts searching to a specific document object or the children of a certain DOM element.
默认情况下,选择器从文档根目录开始在DOM中执行搜索.但是,可以使用$()函数的可选第二个参数为搜索提供备用上下文.例如,如果我们希望在回调函数中搜索某个元素,则可以限制该搜索:
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');
});
由于我们将span选择器限制在此上下文中,因此,只有clicked元素内的span才能获得附加类.
Since we've restricted the span selector to the context of this, only spans within the clicked element will get the additional class.
在显示的示例中,您将在javascript_object
的子代中搜索ID为id
的元素.
In the example you show, you would be searching for the element with the ID id
among the children of javascript_object
.
这篇关于这在jquery $('#id',javascript_object);中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!