jQuery 找到 $.find('selector') 与 $('selector') 的区别 [英] jQuery find $.find('selector') versus $('selector') difference
问题描述
我有一个问题,为什么这两个代码片段不同.
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()
上面的代码返回:一些链接文本
但是
$.find('#ctl00_DDMenu1_HyperLink1')
//[a#ctl00_DDMenu1_HyperLink1 Default.aspx] Consolee output
$.find('#ctl00_DDMenu1_HyperLink1').text()
返回
TypeError: $.find("#ctl00_DDMenu1_HyperLink1").text
不是函数
TypeError:
$.find("#ctl00_DDMenu1_HyperLink1").text
is not a function
这是否意味着$.find
返回Array对象[]
和jQuery函数不可访问?
Does this mean that $.find
return Array object []
and jQuery functions are not accessible?
我用过 jQuery 1.4.2 &使用 Firebug 控制台.
I've used jQuery 1.4.2 & used Firebug Console.
此代码将返回 jQuery 对象引用,并且所有 jQuery 函数都可以访问.
This code will return jQuery object reference and all jQuery function are accessible.
$('any_selector')
//jQuery(item1),jQuery(item2),...,jQuery(item-N) 控制台输出$('any_selector').text()
此代码返回 JavaScript Array 对象,因此 jQuery 的任何函数都不能应用于结果集.即使结果集似乎相同.
This code return JavaScript Array object so any function of jQuery cannot be applied to resultset. Even when resultset seems to be identical.
$.find('any_selector')
//[item1,item2,...,item-N] 控制台输出
$.find('any_selector').text()
但是我们可以做一些技巧(奇怪的技巧)将 js 数组包装到 jQuery 选择器中:
But we can do trick (weird trick) to wrapp js Array into jQuery selector:
$($.find('any_selector_as_inner_select')).val()
//感谢大家的帮助!
推荐答案
这不起作用的原因是因为 find()
允许您根据您的选择过滤一组元素例如,如果您想选择特定表单中的所有输入,您可以编写:
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:
$('#aParticularForm').find('input')
它不能单独调用.
这篇关于jQuery 找到 $.find('selector') 与 $('selector') 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!