dojo/query返回的是匹配查询结果的所有节点构成的数组;这个数组实际上是一个特殊的数组对象称为dojo/NodeList, 该数组对象内建了一系列可以方便操作其中节点的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE html> <html> <head lang= "en" >
<meta charset= "UTF-8" >
<title>NodeList</title>
<script src= "dojo/dojo.js" data-dojo-config= "async: true" ></script>
<script>
require([ "dojo/query" , "dojo/domReady!" ], function (query){
query( "li.fresh" ).on( "click" , function (){
alert( "I love fresh " + this .innerHTML);
});
});
</script>
</head> <body> <ul> <li class = "fresh" >Apples</li>
<li class = "fresh" >Persimmons</li>
<li class = "fresh" >Grapes</li>
<li class = "fresh" >Fresh Figs</li>
<li class = "dried" >Dates</li>
<li class = "dried" >Raisins</li>
<li class = "dried" >Prunes</li>
<li class = "fresh dried" >Apricots</li>
<li class = "fresh" >Peaches</li>
<li class = "fresh" >Bananas</li>
<li class = "fresh" >Cherries</li>
</ul> </body> </html> |
等效于:
1
2
3
4
5
6
7
|
<script> require([ "dojo/query" , "dojo/domReady!" ], function (query){
query( "li.fresh" ).on( "click" , function (e){
alert( "I love fresh " + e.target.innerHTML);
});
});
</script> |
等效于:
1
2
3
4
5
6
7
|
<script>
require([ "dojo/query" , "dojo/on" , "dojo/domReady!" ], function (query,on){
on(query( "li.fresh" ), "click" , function (e){
alert( "I love fresh " + e.target.innerHTML);
});
});
</script> |
备注:dojo/NodeList
对象与DOM NodeList对象是不相同的。
本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1398817,如需转载请自行联系原作者