jQuery 查找 $.find('selector') 与 $('selector') 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6083183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery find $.find('selector') versus $('selector') difference
提问by r.piesnikowski
I've got a question why these two code snippets are different.
我有一个问题,为什么这两个代码片段不同。
$('#ctl00_DDMenu1_HyperLink1')
//jQuery(a#ctl00_DDMenu1_HyperLink1 Default.aspx) Console output
$('#ctl00_DDMenu1_HyperLink1').text()
The code above returns : Some link text
上面的代码返回: Some link text
But
但
$.find('#ctl00_DDMenu1_HyperLink1')
//[a#ctl00_DDMenu1_HyperLink1 Default.aspx] Consolee output
$.find('#ctl00_DDMenu1_HyperLink1').text()
Returns
退货
TypeError:
$.find("#ctl00_DDMenu1_HyperLink1").text
is not a function
类型错误:
$.find("#ctl00_DDMenu1_HyperLink1").text
不是函数
Does this mean that $.find
return Array object []
and jQuery functions are not accessible?
这是否意味着无法访问$.find
返回数组对象[]
和 jQuery 函数?
//EDIT
//编辑
I've used jQuery 1.4.2 & used Firebug Console.
我使用过 jQuery 1.4.2 并使用过 Firebug 控制台。
//Answer found by practise
//实践中找到的答案
This code will return jQuery object referenceand all jQuery function are accessible.
此代码将返回jQuery 对象引用,并且所有 jQuery 函数都可以访问。
$('any_selector')
//jQuery(item1),jQuery(item2),...,jQuery(item-N) Console output
$('any_selector').text()
$('any_selector')
//jQuery(item1),jQuery(item2),...,jQuery(item-N) Console output
$('any_selector').text()
This code return JavaScript Array objectso any function of jQuery cannot be applied to resultset. Even when resultset seems to be identical.
此代码返回JavaScript Array 对象,因此 jQuery 的任何功能都不能应用于结果集。即使结果集似乎相同。
$.find('any_selector')
//[item1,item2,...,item-N] Consolee output
$.find('any_selector').text()
$.find('any_selector')
//[item1,item2,...,item-N] Consolee output
$.find('any_selector').text()
But we can do trick (weird trick) to wrapp js Array into jQuery selector:
但是我们可以使用技巧(奇怪的技巧)将 js 数组包装到 jQuery 选择器中:
$($.find('any_selector_as_inner_select')).val()
$($.find('any_selector_as_inner_select')).val()
//Thanks for help guys!
//感谢大家的帮助!
采纳答案by cm2
The reason this does not work is because find()
lets you filter on a set of elements based on a selection you've already made.For example if you wanted to select all of the inputs within a particular form, you could write:
这不起作用的原因是因为find()
让您根据您已经做出的选择过滤一组元素。例如,如果您想选择特定表单中的所有输入,您可以编写:
$('#aParticularForm').find('input')
It cannot be called on its own.
它不能单独调用。