jQuery .each()返回DOM元素而不是一个jQuery对象 [英] jQuery .each() returns DOM element and not a jQuery object
问题描述
.each()
。 p> 以下将不起作用这个
指DOM元素而不是jQuery对象
$(span [id $ ='_ TotalItemCost'])。each(function(){
var someText = this.text ;
});
修改为将此
转换为jQuery对象并且一切都很好
$(span [id $ ='_ TotalItemCost'])。each(function(){
var someText = $(this).text();
});
我的选择器有什么好玩吗? jQuery .each()
文档错误而且它不是一个jQuery对象,而是由 .each()
返回的DOM元素
该文档没有错,但是您可能会误会jQuery对象是什么。
jQuery对象由 $ ()
函数。所以 $(span [id $ ='_ TotalItemCost'])
是一个jQuery对象,其中包含选择的每个span元素。
使用 .each()
将遍历jQuery对象中包含的元素。这就是为什么这是一个DOM节点,而不是一个jQuery对象。
你使用 $(this)
在此特定元素上使用jQuery方法。
I could be misunderstanding what is happening but from what I can tell I am getting a DOM element and not a jQuery object when I use .each()
.
The below will not work as this
refers to a DOM element and not a jQuery object
$("span[id$='_TotalItemCost']").each(function() {
var someText = this.text();
});
Modified to transform this
to a jQuery object and all is well
$("span[id$='_TotalItemCost']").each(function() {
var someText = $(this).text();
});
Is there something funky with my selector? Is the jQuery .each()
documentation wrong and it's not a jQuery object but rather a DOM element returned by .each()
?
The documention is not wrong but you may misunderstand what a jQuery object is.
The jQuery object is returned by the $()
function. So $("span[id$='_TotalItemCost']")
is one jQuery object which contains every span element selected.
Using .each()
will iterate over the elements contained in the jQuery object. This is why this is a DOM node and not a jQuery object.
You did the right thing by using $(this)
to use the jQuery methods on this specific element.
这篇关于jQuery .each()返回DOM元素而不是一个jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!