编程语言
首页 > 编程语言> > Javascript-Dojo-如何在另一个forEach中的选定节点上使用forEach

Javascript-Dojo-如何在另一个forEach中的选定节点上使用forEach

作者:互联网

在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*/
    });
})

我还自由更改了自变量的名称(因为与jQuery不同,Dojo不会更改此变量),并从forEach中删除了可选的i参数.

标签:foreach,dojo,javascript
来源: https://codeday.me/bug/20191201/2084405.html