在 jQuery 中我会这样做:
var foo = jQuery("form");
foo.each(function(){
this.find(".required").each(function(){...})
})
这会找到当前处理的表单中的每个必填字段。
但是因为我必须使用 Dojo,所以我有点迷路了。在 Dojo 中你需要使用 dojo.forEach()
var foo = dojo.query("form");
dojo.forEach(foo, function(self,i){
// And now I have no idea on what to use the forEach.
// The "self" parameter is the form node. So now I
// would need something like `self.forEach`
// But that obviously does not work
}
最佳答案
你只需要再做一次查询,就像你在第一关所做的那样。 Dojo 中的理念是事物通常没有 jQuery 中的那么“神奇”。
var forms = dojo.query("form"); //No second argument; Searches whole document
dojo.forEach(forms, function(form){
var requireds = dojo.query(".required", form);//Only search under
// the current form.
dojo.forEach(requireds, function(required){
/* do stuff*/
});
})
我还冒昧地更改了 self 变量的名称(因为与 jQuery 不同,Dojo 不会更改 this
)并从中删除了可选的 i
参数forEach。
关于javascript - Dojo - 如何在另一个 forEach 内的选定节点上使用 forEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8716144/