在this question ,使用了以下代码-
var parent = $("#jcontent");
var button1 = $(".button1", parent) ;
在 id jcontent
的父级中选择类 button1
的按钮。
为什么会这样?将 jQuery 对象作为参数传递给 jQuery 选择器如何告诉它在该对象内进行选择?有人可以链接到解释此功能的文档吗?
最佳答案
它是 core method call 的 context
参数.
参数描述为:
A DOM Element, Document, or jQuery to use as context
然后是标有“选择器上下文”的部分,其开头为:
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.
关于javascript - 为什么这个 jQuery 选择器有效?它基于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11127295/