为什么这个jQuery选择器有效?它的基础是什么?

时间:2022-11-20 16:55:22

In this question, the following code is used -

在这个问题中,使用以下代码 -

var parent = $("#jcontent"); 
var button1 = $(".button1", parent) ;

to select a button of class button1 within the parent of id jcontent.

在id jcontent的父级中选择类button1的按钮。

Why does this work? How does passing a jQuery object as a parameter to a jQuery selector tell it to select within that object? Can someone link to the docs which explain this function?

为什么这样做?如何将jQuery对象作为参数传递给jQuery选择器告诉它在该对象中进行选择?有人可以链接到解释此功能的文档吗?

2 个解决方案

#1


6  

It's the context parameter for the core method call.

它是核心方法调用的上下文参数。

The parameter is described as:

该参数描述为:

A DOM Element, Document, or jQuery to use as context

用作上下文的DOM元素,文档或jQuery

And then there's a section labelled "Selector Context," which starts with:

然后有一个标记为“选择器上下文”的部分,它以:

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.

默认情况下,选择器在DOM中从文档根开始执行搜索。但是,通过使用$()函数的可选第二个参数,可以为搜索提供备用上下文。

#2


5  

The second parameter is the selector context: the DOM element, document, or jQuery object inside which the selector is matched. In the absence of this parameter, document root is assumed.

第二个参数是选择器上下文:选择器匹配的DOM元素,文档或jQuery对象。如果没有此参数,则假定文档根。

The following statement:

以下声明:

var button1 = $(".button1", parent); // parent = $("#jcontent")

is same as writing **:

与写**相同:

var button1 = parent.find(".button1"); // parent = $("#jcontent")

and (in this case) produces results identical to this:

和(在这种情况下)产生与此相同的结果:

var button1 = $("#jcontent .button1");

** as mentioned here:

**如上所述:

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

在内部,选择器上下文是使用.find()方法实现的,因此$('span',this)等价于$(this).find('span')。

#1


6  

It's the context parameter for the core method call.

它是核心方法调用的上下文参数。

The parameter is described as:

该参数描述为:

A DOM Element, Document, or jQuery to use as context

用作上下文的DOM元素,文档或jQuery

And then there's a section labelled "Selector Context," which starts with:

然后有一个标记为“选择器上下文”的部分,它以:

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.

默认情况下,选择器在DOM中从文档根开始执行搜索。但是,通过使用$()函数的可选第二个参数,可以为搜索提供备用上下文。

#2


5  

The second parameter is the selector context: the DOM element, document, or jQuery object inside which the selector is matched. In the absence of this parameter, document root is assumed.

第二个参数是选择器上下文:选择器匹配的DOM元素,文档或jQuery对象。如果没有此参数,则假定文档根。

The following statement:

以下声明:

var button1 = $(".button1", parent); // parent = $("#jcontent")

is same as writing **:

与写**相同:

var button1 = parent.find(".button1"); // parent = $("#jcontent")

and (in this case) produces results identical to this:

和(在这种情况下)产生与此相同的结果:

var button1 = $("#jcontent .button1");

** as mentioned here:

**如上所述:

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

在内部,选择器上下文是使用.find()方法实现的,因此$('span',this)等价于$(this).find('span')。