dojo.query() is the swiss army knife of DOM node manipulation in Dojo. Much like Prototype’s “$$” (bling-bling) function or JQuery’s “$” function,dojo.query provides robust,high-performance css-based node selector support with the option of scoping searches to a particular sub-tree of a document
简单点说,就是dojo.query()是可以按样式、标签名、字符等等,来查询文档中的某个元素。
关键是怎么使用,国外的网上说的很清楚,其实你要是能耐住性子把DOJO BOOK里面的都认认真真看一边,就不用看我下面写的这点东西。
我也是惊叹dojo.query 中文资料的匮乏,才把自己看的英文资料和自己的实践做个小结,利人利己。
这个是老外的例子:
Example 1
search the entire document for elements with the class “foo”:
dojo.query(".foo");
these elements will match:
Example 2
search the entire document for elements with the classes “foo” and “bar”:
dojo.query(".foo.bar");
these elements will match:
while these will not:
Example 3
find “ elements which are descendants of paragraphs and which have a “highlighted” class:
dojo.query("p span.highlighted");
the innermost span in this fragment matches:
...
...
Example 4
set an “odd” class on all odd table rows inside of the table
#tabular_data
,using the
>
(direct child) selector to avoid affecting any nested tables:
dojo.query("#tabular_data > tbody > tr:nth-child(odd)").addClass("odd");
Example 5
remove all elements with the class “error” from the document and store them in a list:
var errors = dojo.query(".error").orphan();
Example 6
add an onclick handler to every submit button in the document which causes the form to be sent via Ajax instead:
dojo.query("input[type='submit']").onclick(function(e){
dojo.stopEvent(e); // prevent sending the form
var btn = e.target;
dojo.xhrPost({
form: btn.form,
load: function(data){
// replace the form with the response
var div = dojo.doc.createElement("div");
dojo.place(div,btn.form,"after");
div.innerHTML = data;
dojo.style(btn.form,"display","none");
}
});
});
上面这些例子主要是为了大家能了解query,怎么用!
下面是我自己的例子:
结合JSON查询。
这个是个JSON集合,比较好的用法是你把这个代码放在一个filename.json的文件里。
我放在D:\WK50\tems114\test.json