Heim > Web-Frontend > js-Tutorial > Hauptteil

Ext.get() 和 Ext.query()组合使用实现最灵活的取元素方式_jquery

WBOY
Freigeben: 2016-05-16 18:01:46
Original
1193 Leute haben es durchsucht

前面写的get()和query()我都省略参数了,先看看文档中的函数原型:
Ext.get( Mixed el ) : Element
Parameters:
el : Mixed
The id of the node, a DOM Node or an existing Element.
Returns:
Element
The Element object
Ext.query( String path, [Node root] ) : Array
Parameters:
path : String
The selector/xpath query
root : Node
(optional) The start of the query (defaults to document).
Returns:
Array
query函数返回的其实是一个DOM Node的数组,而Ext.get的参数el可以是DOM Node,哈哈,明白了吗?就是说要实现最灵活的取法,应该用query取到DOM Node然后交给get去变成Element。也就是:
var x=Ext.query(QueryStr);
//我为什么不写成内联函数形式?因为这里的x只能是一个元素,而上面那句的x是一个Array,大家自己转换和处理吧
var y=Ext.get(x);
那么接下来需要介绍QueryStr的格式(其实和jQuery里的selector的格式很像啦),至于获得Element后可以干些啥,大家自己去看ExtJS文档里关于Ext.Element的说明,我就不摘过来了。
先给一个html代码,好做演示说明

复制代码 代码如下:




I'm a div ==> my id: bar, my class: foo
I'm a span within the div with a foo class
An ExtJs link


my id: foo, my class: bar

I'm a P tag within the foo div


I'm a span within the div with a bar class
An internal link

BlueLotus7@126.com




(1)根据标记取:// 这个查询会返回有两个元素的数组因为查询选中对整个文档的所有span标签。
Ext.query("span");
// 这个查询会返回有一个元素的数组因为查询顾及到了foo这个id。
Ext.query("span", "foo");// 这会返回有一个元素的数组,内容为div标签下的p标签
Ext.query("div p");
// 这会返回有两个元素的数组,内容为div标签下的span标签
Ext.query("div span");(2)根据ID取:// 这个查询会返回包含我们foo div一个元素的数组!
Ext.query("#foo"); //或者直接Ext.get("foo");(3)根据class的Name去取:Ext.query(".foo");// 这个查询会返回5个元素的数组。
Ext.query("*[class]"); // 结果: [body#ext-gen2.ext-gecko, div#bar.foo, span.bar, div#foo.bar, span.bar](4)万能法去取:(用这个方法可以通过id、name、class、css等取)// 这会得到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]");//这会得到name等于“BlueLotus7”的所有元素
Ext.query("*[name=BlueLotus7]");
我们换个html代码:
复制代码 代码如下:






我是一个div ==> 我的id是: bar, 我的class: foo
I'm a span within the div with a foo class
An ExtJs link with a blank target!


my id: foo, my class: bar

I'm a P tag within the foo div


I'm a span within the div with a bar class
An internal link




// 获取所以红色的元素
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, a www.extjs.com, div#foo.bar, p, span.bar, a test.html#]
// 获取所有颜色属性是从“yel”开始的元素
Ext.query("*{color^=yel}"); // [a www.extjs.com]
// 获取所有颜色属性是以“ow”结束的元素
Ext.query("*{color$=ow}"); // [a www.extjs.com]
// 获取所有颜色属性包含“ow”字符的元素
Ext.query("*{color*=ow}"); // [a www.extjs.com, span.bar]
(5)伪操作符取法换个html:
复制代码 代码如下:






我是一个div ==> 我的id是bar,我的class是foo
这里是span元素,外层的div元素有foo的class属性
设置blank=target的ExtJS链接


这里的id是:foo,这里的class是bar

“foo” div包围下的p元素。


这里是一个span元素,外层是div包围着,span还有一个bar的class属性。
内置链接



  • 条目 #1

  • 条目 #2

  • 条目 #3

  • 条目 #4 带有链接














第一行,第一列 第一行,第二列
第二行,已合并单元格!
第三行,第一列 第三行,第二列














//SPAN元素为其父元素的第一个子元素
Ext.query("span:first-child"); // [span.bar]
//A元素为其父元素的最后一个子元素
Ext.query("a:last-child") // [a www.extjs.com, a test.html#]
//SPAN元素为其父元素的第2个子元素(由1开始的个数)
Ext.query("span:nth-child(2)") // [span.bar]
//TR元素为其父元素的奇数个数的子元素
Ext.query("tr:nth-child(odd)") // [tr, tr]
//LI元素为其父元素的奇数个数的子元素
Ext.query("li:nth-child(even)") // [li, li]
//返回A元素,A元素为其父元素的唯一子元素
Ext.query("a:only-child") // [a test.html#]
//返回所有选中的(checked)的INPUT元素
Ext.query("input:checked") // [input#chked on]
//返回第一个的TR元素
Ext.query("tr:first") // [tr]
//返回最后一个的INPUT元素
Ext.query("input:last") // [input#notChked on]
//返回第二个的TD元素
Ext.query("td:nth(2)") // [td]
//返回每一个包含“within”字符串的DIV
Ext.query("div:contains(within)") // [div#bar.foo, div#foo.bar]
//返回没有包含FORM子元素以外的那些DIV
Ext.query("div:not(form)") [div#bar.foo, div#foo.bar, div]
//返回包含有A元素的那些DIV集合
Ext.query("div:has(a)") // [div#bar.foo, div#foo.bar, div]
//返回接着会继续有TD的那些TD集合。尤其一个地方是,如果使用了colspan属性的TD便会忽略
Ext.query("td:next(td)") // [td, td]
//返回居前于INPUT元素的那些LABEL元素集合
Ext.query("label:prev(input)") //[label, label]
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!