JQuery循环遍历JQueryObjects [英] JQuery Loop over JQueryObjects
问题描述
我有一个方法,它将接受JQuery对象的参数并计算一个部分的总数。因此,如果你给它一个包含该部分的div的JQuery对象,它将为它计算总数
I have a method, which will accept a parameter of a JQuery Object and will calculate totals for a section. So if you give it a JQuery Object of a div containing the section it will calculate a total for it
所以你可以这样做:
var $ totalcompletion = CalculateSectionCompletion(jQuery(#Section1));
var $totalcompletion = CalculateSectionCompletion(jQuery("#Section1"));
现在我有多个带有section容器类的div。我希望能够在该类的任何div上调用上述方法。
Now I have multiple divs with the class of section container. I want to be able to call the above method on any div with that class.
我这样做:
jQuery(div.SectionContainer)。each(
function(i,valueOfElement){
CalculateSectionCompletion(valueOfElement);
});
jQuery("div.SectionContainer").each(
function(i, valueOfElement){
CalculateSectionCompletion(valueOfElement);
});
问题是valueOfElement实际上是DOM对象而不是JQuery对象,所以我无法将其传递给我的方法。
The problem is the valueOfElement is actually the DOM object and not the JQuery Object, so I can't pass this in to my method.
无论如何,我可以遍历查询选择的所有JQuery对象,而无需编写一些脏代码从DOM对象中提取Id,并调用JQuery(valueOfElement.id)并将其传入?
Is there anyway I can loop through all JQuery Objects selected by a query, without writing some dirty code to extract the Id from the DOM object, and call JQuery(valueOfElement.id) and pass it in?
推荐答案
您可以将$元素包装在$(..)中,就像使用$(文档)一样。
You can wrap any DOM element in $(..), as you do with $(document).
所以我认为你应该能够
jQuery("div.SectionContainer").each( function(i, valueOfElement){
CalculateSectionCompletion($(valueOfElement));
});
这篇关于JQuery循环遍历JQueryObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!