Tutorial:DomQuery Basics(DomQuery的基础)
DomQuery基础
DomQuery的select函数有两个参数。第一个是选择符字符(selector string )而第二个是欲生成查询的标签ID(TAG ID)。本文中我准备使用函数“Ext.query”但读者须谨记它是“Ext.DomQuery.select()”的简写方式。
这是要入手的html:
- <html>
- <head>
- <script type="text/javascript" src= "../js/firebug/firebug.js" ></script>
- </head>
- <body>
- <script type="text/javascript" src= "../ext/ext-base.js" ></script>
- <script type="text/javascript" src= "../ext/ext-core.js" ></script>
- <div id="bar" class = "foo" >
- I'm a div ==> my id: bar, my class : foo
- <span class = "bar" >I'm a span within the div with a foo class </span>
- <a href="http://www.extjs.com" target= "_blank" >An ExtJs link</a>
- </div>
- <div id="foo" class = "bar" >
- my id: foo, my class : bar
- <p>I'm a P tag within the foo div</p>
- <span class = "bar" >I'm a span within the div with a bar class </span>
- <a href="#" >An internal link</a>
- </div>
- </body>
- </hmlt>
<html> <head> <script type="text/javascript" src="../js/firebug/firebug.js"></script> </head> <body> <script type="text/javascript" src="../ext/ext-base.js"></script> <script type="text/javascript" src="../ext/ext-core.js"></script> <div id="bar" class="foo"> I'm a div ==> my id: bar, my class: foo <span class="bar">I'm a span within the div with a foo class</span> <a href="http://www.extjs.com" target="_blank">An ExtJs link</a> </div> <div id="foo" class="bar"> my id: foo, my class: bar <p>I'm a P tag within the foo div</p> <span class="bar">I'm a span within the div with a bar class</span> <a href="#">An internal link</a> </div> </body> </hmlt>
第一部分:元素选择符Selector
假设我想获取文档内所有的“span”标签:
- // 这个查询会返回有两个元素的数组因为查询选中对整个文档的所有span标签。
- Ext.query("span" );
- // 这个查询会返回有一个元素的数组因为查询顾及到了foo这个id。
- Ext.query("span" , "foo" );注意刚才怎么传入一个普通的字符串作为第一个参数。
- 按id获取标签,你需要加上“#”的前缀:
- // 这个查询会返回包含我们foo div一个元素的数组!
- Ext.query("#foo" );按 class name获取标签,你需要加上“.”的前缀:
- /*这个查询会返回有一个元素的数组,
- 包含与之前例子一样的div但是我们使用了class name来获取*/
- Ext.query(".foo" );你也可以使用关键字“*”来获取所有的元素:
- // 这会返回一个数组,包含文档的所有元素。
- Ext.query("*" );要获取子标签,我们只须在两个选择符之间插入一个空格:
- // 这会返回有一个元素的数组,包含p标签的div标签
- Ext.query("div p" );
- // 这会返回有两个元素的数组,包含span标签的div标签
- Ext.query("div span" );
// 这个查询会返回有两个元素的数组因为查询选中对整个文档的所有span标签。 Ext.query("span"); // 这个查询会返回有一个元素的数组因为查询顾及到了foo这个id。 Ext.query("span", "foo");注意刚才怎么传入一个普通的字符串作为第一个参数。 按id获取标签,你需要加上“#”的前缀: // 这个查询会返回包含我们foo div一个元素的数组! Ext.query("#foo");按class name获取标签,你需要加上“.”的前缀: /*这个查询会返回有一个元素的数组, 包含与之前例子一样的div但是我们使用了class name来获取*/ Ext.query(".foo");你也可以使用关键字“*”来获取所有的元素: // 这会返回一个数组,包含文档的所有元素。 Ext.query("*");要获取子标签,我们只须在两个选择符之间插入一个空格: // 这会返回有一个元素的数组,包含p标签的div标签 Ext.query("div p"); // 这会返回有两个元素的数组,包含span标签的div标签 Ext.query("div span");
还有三个的元素选择符,待后续的教程会叙述。 ""
如果朋友你觉得这里说得太简单的话,你可以选择到DomQuery 文档看看,可能会有不少收获:)
第二部分:属性选择符Attributes selectors
这些选择符可让你得到基于一些属性值的元素。属性指的是html元素中的href, id 或 class。
- // 我们检查出任何存在有class属性的元素。
- // 这个查询会返回5个元素的数组。
- Ext.query("*[class]" );
- // 结果: [body#ext-gen2.ext-gecko, div#bar.foo, span.bar, div#foo.bar, span.bar]
- //现在我们针对特定的class属性进行搜索。
- // 这会得到class等于“bar”的所有元素
- Ext.query("*[class=bar]" );
- // 这会得到class不等于“bar”的所有元素
- Ext.query("*[class!=bar]" );
- // 这会得到class从“b”字头开始的所有元素
- Ext.query("*[class^=b]" );
- //这会得到class由“r”结尾的所有元素
- Ext.query("*[class$=r]" );
- //这会得到在class中抽出“a”字符的所有元素
- Ext.query("*[class*=a]" );
// 我们检查出任何存在有class属性的元素。 // 这个查询会返回5个元素的数组。 Ext.query("*[class]"); // 结果: [body#ext-gen2.ext-gecko, div#bar.foo, span.bar, div#foo.bar, span.bar] //现在我们针对特定的class属性进行搜索。 // 这会得到class等于“bar”的所有元素 Ext.query("*[class=bar]"); // 这会得到class不等于“bar”的所有元素 Ext.query("*[class!=bar]"); // 这会得到class从“b”字头开始的所有元素 Ext.query("*[class^=b]"); //这会得到class由“r”结尾的所有元素 Ext.query("*[class$=r]"); //这会得到在class中抽出“a”字符的所有元素 Ext.query("*[class*=a]");
第三部分: CSS值元素选择符
这些选择符会匹配DOM元素的style属性。尝试在那份html中加上一些颜色:
- <html>
- <head>
- <script type="text/javascript" src= "../js/firebug/firebug.js" ></script>
- </head>
- <body>
- <script type="text/javascript" src= "../ext/ext-base.js" ></script>
- <script type="text/javascript" src= "../ext/ext-core.js" ></script>
- <div id="bar" class = "foo" style= "color:red;" >
- 我是一个div ==> 我的id是: bar, 我的class : foo
- <span class = "bar" style= "color:pink;" >
- I'm a span within the div with a foo class </span>
- <a href="http://www.extjs.com" target= "_blank" style= "color:yellow;" >
- An ExtJs link with a blank target!</a>
- </div>
- <div id="foo" class = "bar" style= "color:fushia;" >
- my id: foo, my class : bar
- <p>I'm a P tag within the foo div</p>
- <span class = "bar" style= "color:brown;" >
- I'm a span within the div with a bar class </span>
- <a href="#" style= "color:green;" >An internal link</a>
- </div>
- </body>
- </html>
<html> <head> <script type="text/javascript" src="../js/firebug/firebug.js"></script> </head> <body> <script type="text/javascript" src="../ext/ext-base.js"></script> <script type="text/javascript" src="../ext/ext-core.js"></script> <div id="bar" class="foo" style="color:red;"> 我是一个div ==> 我的id是: bar, 我的class: foo <span class="bar" style="color:pink;"> I'm a span within the div with a foo class</span> <a href="http://www.extjs.com" target="_blank" style="color:yellow;"> An ExtJs link with a blank target!</a> </div> <div id="foo" class="bar" style="color:fushia;"> my id: foo, my class: bar <p>I'm a P tag within the foo div</p> <span class="bar" style="color:brown;"> I'm a span within the div with a bar class</span> <a href="#" style="color:green;">An internal link</a> </div> </body> </html>
基于这个CSS的颜色值我们不会作任何查询,但可以是其它的内容。它的格式规定是这样的:
元素{属性 操作符 值}
注意我在这里是怎么插入一个不同的括号。
所以,操作符(operators)和属性选择符(attribute selectors)是一样的。
- // 获取所以红色的元素
- Ext.query("*{color=red}" ); // [div#bar.foo]
- // 获取所有粉红颜色的并且是有红色子元素的元素
- Ext.query("*{color=red} *{color=pink}" ); // [span.bar]
- // 获取所有不是红色文字的元素
- Ext.query("*{color!=red}" );
- // [html, head, script firebug.js, link, body#ext-gen2.ext-gecko,
- //script ext-base.js, script ext-core.js, span.bar,
- //div#foo.bar, p, span.bar, a test.html#]
- // 获取所有颜色属性是从“yel”开始的元素
- Ext.query("*{color^=yel}" );
- // 获取所有颜色属性是以“ow”结束的元素
- Ext.query("*{color$=ow}" );
- // 获取所有颜色属性包含“ow”字符的元素
- Ext.query("*{color*=ow}" );
// 获取所以红色的元素 Ext.query("*{color=red}"); // [div#bar.foo] // 获取所有粉红颜色的并且是有红色子元素的元素 Ext.query("*{color=red} *{color=pink}"); // [span.bar] // 获取所有不是红色文字的元素 Ext.query("*{color!=red}"); // [html, head, script firebug.js, link, body#ext-gen2.ext-gecko, //script ext-base.js, script ext-core.js, span.bar, //div#foo.bar, p, span.bar, a test.html#] // 获取所有颜色属性是从“yel”开始的元素 Ext.query("*{color^=yel}"); // 获取所有颜色属性是以“ow”结束的元素 Ext.query("*{color$=ow}"); // 获取所有颜色属性包含“ow”字符的元素 Ext.query("*{color*=ow}");
第四部分:伪类选择符Pseudo Classes selectors
仍然是刚才的网页,但是有所不同的只是新加上了一个UL元素、一个TABLE元素和一个FORM元素,以便我们可以使用不同的伪类选择符,来获取节点。
- <html>
- <head>
- <script type="text/javascript" src= "../js/firebug/firebug.js" ></script>
- </head>
- <body>
- <script type="text/javascript" src= "../ext/ext-base.js" ></script>
- <script type="text/javascript" src= "../ext/ext-core.js" ></script>
- <div id="bar" class = "foo" style="color:red;
- border: 2px dotted red; margin:5px; padding:5px;">
- I'm a div ==> my id: bar, my class : foo
- <span class = "bar" style= "color:pink;" >
- I'm a span within the div with a foo class </span>
- <a href="http://www.extjs.com" target= "_blank" style= "color:yellow;" >
- An ExtJs link with a blank target!</a>
- </div>
- <div id="foo" class = "bar" style="color:fushia;
- border: 2px dotted black; margin:5px; padding:5px;">
- my id: foo, my class : bar
- <p>I'm a P tag within the foo div</p>
- <span class = "bar" style= "color:brown;" >
- I'm a span within the div with a bar class </span>
- <a href="#" style= "color:green;" >An internal link</a>
- </div>
- <div style="border:2px dotted pink; margin:5px; padding:5px;" >
- <ul>
- <li>Some choice #1</li>
- <li>Some choice #2</li>
- <li>Some choice #3</li>
- <li>Some choice #4 with a <a href= "#" >link</a></li>
- </ul>
- <table style="border:1px dotted black;" >
- <tr style="color:pink" >
- <td>1st row, 1st column</td>
- <td>1st row, 2nd column</td>
- </tr>
- <tr style="color:brown" >
- <td colspan="2" >2nd row, colspanned! </td>
- </tr>
- <tr>
- <td>3rd row, 1st column</td>
- <td>3rd row, 2nd column</td>
- </tr>
- </table>
- </div>
- <div style="border:2px dotted red; margin:5px; padding:5px;" >
- <form>
- <input id="chked" type= "checkbox" checked/>
- <label for = "chked" >I'm checked</label>
- <br /><br />
- <input id="notChked" type= "checkbox" /><label for = "notChked" >
- not me brotha!</label>
- </form>
- </div>
- </body>
- </html>
<html> <head> <script type="text/javascript" src="../js/firebug/firebug.js"></script> </head> <body> <script type="text/javascript" src="../ext/ext-base.js"></script> <script type="text/javascript" src="../ext/ext-core.js"></script> <div id="bar" class="foo" style="color:red; border: 2px dotted red; margin:5px; padding:5px;"> I'm a div ==> my id: bar, my class: foo <span class="bar" style="color:pink;"> I'm a span within the div with a foo class</span> <a href="http://www.extjs.com" target="_blank" style="color:yellow;"> An ExtJs link with a blank target!</a> </div> <div id="foo" class="bar" style="color:fushia; border: 2px dotted black; margin:5px; padding:5px;"> my id: foo, my class: bar <p>I'm a P tag within the foo div</p> <span class="bar" style="color:brown;"> I'm a span within the div with a bar class</span> <a href="#" style="color:green;">An internal link</a> </div> <div style="border:2px dotted pink; margin:5px; padding:5px;"> <ul> <li>Some choice #1</li> <li>Some choice #2</li> <li>Some choice #3</li> <li>Some choice #4 with a <a href="#">link</a></li> </ul> <table style="border:1px dotted black;"> <tr style="color:pink"> <td>1st row, 1st column</td> <td>1st row, 2nd column</td> </tr> <tr style="color:brown"> <td colspan="2">2nd row, colspanned! </td> </tr> <tr> <td>3rd row, 1st column</td> <td>3rd row, 2nd column</td> </tr> </table> </div> <div style="border:2px dotted red; margin:5px; padding:5px;"> <form> <input id="chked" type="checkbox" checked/> <label for="chked">I'm checked</label> <br /><br /> <input id="notChked" type="checkbox" /><label for="notChked"> not me brotha!</label> </form> </div> </body> </html>
off we go:
- /*
- this one gives us the first SPAN child of its parent
- */
- Ext.query("span:first-child" ); // [span.bar]
- /*
- this one gives us the last A child of its parent
- */
- Ext.query("a:last-child" ) // [a, a test.html#]
- /*
- this one gives us the second SPAN child of its parent
- */
- Ext.query("span:nth-child(2)" ) // [span.bar]
- /*
- this one gives us ODD TR of its parents
- */
- Ext.query("tr:nth-child(odd)" ) // [tr, tr]
- /*
- this one gives us even LI of its parents
- */
- Ext.query("li:nth-child(even)" ) // [li, li]
- /*
- this one gives us A that are the only child of its parents
- */
- Ext.query("a:only-child" ) // [a test.html#]
- /*
- this one gives us the checked INPUT
- */
- Ext.query("input:checked" ) // [input#chked on]
- /*
- this one gives us the first TR
- */
- Ext.query("tr:first" ) // [tr]
- /*
- this one gives us the last INPUT
- */
- Ext.query("input:last" ) // [input#notChked on]
- /*
- this one gives us the 2nd TD
- */
- Ext.query("td:nth(2)" ) // [td]
- /*
- this one gives us every DIV that has the "within" string
- */
- Ext.query("div:contains(within)" ) // [div#bar.foo, div#foo.bar]
- /*
- this one gives us every DIV that doesn't have a FORM child
- */
- Ext.query("div:not(form)" ) [div#bar.foo, div#foo.bar, div]
- /*
- This one gives use every DIV that has an A child
- */
- Ext.query("div:has(a)" ) // [div#bar.foo, div#foo.bar, div]
- /*
- this one gives us every TD that is followed by another TD.
- obviously, the one that has a colspan property is ignored.
- */
- Ext.query("td:next(td)" ) // [td, td]
- /*
- this one gives us every LABEL that is preceded by an INPUT
- */
- Ext.query("label:prev(input)" ) //[label, label]
/* this one gives us the first SPAN child of its parent */ Ext.query("span:first-child"); // [span.bar] /* this one gives us the last A child of its parent */ Ext.query("a:last-child") // [a, a test.html#] /* this one gives us the second SPAN child of its parent */ Ext.query("span:nth-child(2)") // [span.bar] /* this one gives us ODD TR of its parents */ Ext.query("tr:nth-child(odd)") // [tr, tr] /* this one gives us even LI of its parents */ Ext.query("li:nth-child(even)") // [li, li] /* this one gives us A that are the only child of its parents */ Ext.query("a:only-child") // [a test.html#] /* this one gives us the checked INPUT */ Ext.query("input:checked") // [input#chked on] /* this one gives us the first TR */ Ext.query("tr:first") // [tr] /* this one gives us the last INPUT */ Ext.query("input:last") // [input#notChked on] /* this one gives us the 2nd TD */ Ext.query("td:nth(2)") // [td] /* this one gives us every DIV that has the "within" string */ Ext.query("div:contains(within)") // [div#bar.foo, div#foo.bar] /* this one gives us every DIV that doesn't have a FORM child */ Ext.query("div:not(form)") [div#bar.foo, div#foo.bar, div] /* This one gives use every DIV that has an A child */ Ext.query("div:has(a)") // [div#bar.foo, div#foo.bar, div] /* this one gives us every TD that is followed by another TD. obviously, the one that has a colspan property is ignored. */ Ext.query("td:next(td)") // [td, td] /* this one gives us every LABEL that is preceded by an INPUT */ Ext.query("label:prev(input)") //[label, label]
总结
API依然最重要的资讯来源。本篇教程做的仅仅是拿一张现实中的网页示范了一些结果。
如读者已了解过API的DomQuery内容,可跳过本文,直接阅读 DomQuery advanced tutorial!
译者姓名:Frank
译者博客:http://www.ajaxjs.com/blog/
Tutorial:DomQuery Basics(DomQuery的基础)相关推荐
- Adobe Dimension Essential Training: The Basics Adobe Dimension 基础教程:基础知识 Lynda课程中文字幕
Adobe Dimension Essential Training: The Basics 中文字幕 Adobe Dimension 基础教程:基础知识 中文字幕Adobe Dimension Es ...
- Z-Wave Networking Basics ZWAVE网络基础
Z-Wave Networking Basics qq:380939960 Z-Wave的网络层定义了一个由最多可以容纳231个设备组成的网络,适合大多数住宅和轻型商业应用.他们定义一个网状网络拓扑( ...
- java编程语言基础外文,Java编程语言基础(外文文献翻译)
Java编程语言基础(外文文献翻译) JavaTM Programming Language Basics Like applications, applets are created from cl ...
- vtk 转换视角_vtk使用基础[1]
HuangSteve@163.com 第 1 页 共 10 页 第四章 The Basics (VTK 使用基础 ) yefeng2005 一.主要内容: VTK 是由 Will Schroeder ...
- Grbl V1.1F-GRBL接口基础
# Grbl Interface Basics Grbl接口基础 The interface for Grbl is fairly simple and straightforward. With ...
- 2007-11-7学习EXT第一天:EXT简介
原文出处 翻译:Frank 无论你是Ext库的新手,抑或是想了解Ext的人,本篇文章的内容都适合你.本文将简单地介绍Ext的几个基本概念,和如何快速地做出一个动态的页面并运行起来,假设读者已具备了一些 ...
- 学习Ext第一天(Ext 简介)
无论你是Ext库的新手,抑或是想了解Ext的人,本篇文章的内容都适合你.本文将简单地介绍Ext的几个基本概念,和如何快速地做出一个动态的页面并运行起来,假设读者已具备了一些JavaScript经验和初 ...
- Ext2.0学习入门
本教程适用于Ext 2.0的版本,而版本1.x仍可找到. 无论你是Ext库的新手,抑或是想了解Ext的人,本篇文章的内容都适合你.本文将简单地介绍Ext的几个基本概念,和如何快速地做出一个动态的页面并 ...
- 计算机编程课程顺序_您可以在6月开始参加630项免费的在线编程和计算机科学课程...
计算机编程课程顺序 Six years ago, universities like MIT and Stanford first opened up free online courses to t ...
最新文章
- 设置root密码,su与sudo的区别
- Firebug高级用法 - Web开发的利器
- npm ERR! the command again as root/Administrator
- U盘版便携式Linux制作, casper-rw 解析
- Deepin下java开发环境部署
- [.Net线程处理系列]专题五:线程同步——事件构造
- bootstrap-multiselect.js多选下拉框初始化时默认选中初始值
- 机器学习之常用优化方法(GD、牛顿、拟牛顿、拉格朗日乘子)
- python官方文档怎么样_python官方文档
- 写出常用的5个linux命令 并解释,【PHP面试题】写出尽可能多的Linux命令。
- HDOJ 1671 HDU 1671 Phone List ACM 1671 IN HDU
- 织梦php集成环境安装包,常用PHP运行环境一键安装包
- 苹果apple id无法申请开发者帐号问题
- zdragon 厚积薄发(博客)
- 魔兽世界开服教程——魔兽世界服务器架设全攻略---战网+Ladder排行版
- 解决“该文件夹包含名称过长且无法放入回收站的项目”导致无法删除
- matlab将图片旋转的代码_空间曲线绕空间直线旋转生成的旋转曲面方程
- Nginx下同域部署多个Vue项目(history路由模式),报404、500错误
- ResNet详细介绍
- java中jar文件