Ext.get() 和 Ext.query()組合使用實現(xiàn)最靈活的取元素方式
更新時間:2011年09月26日 22:22:32 作者:
想要利用ExtJS的庫函數(shù)對DOM進行各類操作,就要得到Element類型的對象,但是Ext.get()取到的雖然是Element,但是參數(shù)只能是id,如果大家對jQuery的selector方式很喜歡和崇拜,那么就一定要學習Ext.get()和Ext.query()的組合方式。
前面寫的get()和query()我都省略參數(shù)了,先看看文檔中的函數(shù)原型:
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函數(shù)返回的其實是一個DOM Node的數(shù)組,而Ext.get的參數(shù)el可以是DOM Node,哈哈,明白了嗎?就是說要實現(xiàn)最靈活的取法,應該用query取到DOM Node然后交給get去變成Element。也就是:
var x=Ext.query(QueryStr);
//我為什么不寫成內(nèi)聯(lián)函數(shù)形式?因為這里的x只能是一個元素,而上面那句的x是一個Array,大家自己轉(zhuǎn)換和處理吧
var y=Ext.get(x);
那么接下來需要介紹QueryStr的格式(其實和jQuery里的selector的格式很像啦),至于獲得Element后可以干些啥,大家自己去看ExtJS文檔里關于Ext.Element的說明,我就不摘過來了。
先給一個html代碼,好做演示說明
<html>
<body>
<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 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>
<div name="BlueLotus7">BlueLotus7@126.com</div>
</body>
</hmlt>
(1)根據(jù)標記?。?/ 這個查詢會返回有兩個元素的數(shù)組因為查詢選中對整個文檔的所有span標簽。
Ext.query("span");
// 這個查詢會返回有一個元素的數(shù)組因為查詢顧及到了foo這個id。
Ext.query("span", "foo");// 這會返回有一個元素的數(shù)組,內(nèi)容為div標簽下的p標簽
Ext.query("div p");
// 這會返回有兩個元素的數(shù)組,內(nèi)容為div標簽下的span標簽
Ext.query("div span");(2)根據(jù)ID?。?/ 這個查詢會返回包含我們foo div一個元素的數(shù)組!
Ext.query("#foo"); //或者直接Ext.get("foo");(3)根據(jù)class的Name去?。篍xt.query(".foo");// 這個查詢會返回5個元素的數(shù)組。
Ext.query("*[class]"); // 結(jié)果: [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”結(jié)尾的所有元素
Ext.query("*[class$=r]");
//這會得到在class中抽出“a”字符的所有元素
Ext.query("*[class*=a]");//這會得到name等于“BlueLotus7”的所有元素
Ext.query("*[name=BlueLotus7]");
我們換個html代碼:
<html>
<head>
</head>
<body>
<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 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>
// 獲取所以紅色的元素
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”結(jié)束的元素
Ext.query("*{color$=ow}"); // [a www.extjs.com]
// 獲取所有顏色屬性包含“ow”字符的元素
Ext.query("*{color*=ow}"); // [a www.extjs.com, span.bar]
(5)偽操作符取法換個html:
<html>
<head>
</head>
<body>
<div id="bar" class="foo" style="color:red; border: 2px dotted red; margin:5px; padding:5px;">
我是一個div ==> 我的id是bar,我的class是foo
<span class="bar" style="color:pink;">這里是span元素,外層的div元素有foo的class屬性</span>
<a target="_blank" style="color:yellow;">設置blank=target的ExtJS鏈接</a>
</div>
<div id="foo" class="bar" style="color:fushia; border: 2px dotted black; margin:5px; padding:5px;">
這里的id是:foo,這里的class是bar
<p>“foo” div包圍下的p元素。</p>
<span class="bar" style="color:brown;">這里是一個span元素,外層是div包圍著,span還有一個bar的class屬性。</span>
<a href="#" style="color:green;">內(nèi)置鏈接</a>
</div>
<div style="border:2px dotted pink; margin:5px; padding:5px;">
<ul>
<li>條目 #1</li>
<li>條目 #2</li>
<li>條目 #3</li>
<li>條目 #4 帶有<a href="#">鏈接</a></li>
</ul>
<table style="border:1px dotted black;">
<tr style="color:pink">
<td>第一行,第一列</td>
<td>第一行,第二列</td>
</tr>
<tr style="color:brown">
<td colspan="2">第二行,已合并單元格!</td>
</tr>
<tr>
<td>第三行,第一列</td>
<td>第三行,第二列</td>
</tr>
</table>
</div>
<div style="border:2px dotted red; margin:5px; padding:5px;">
<form>
<input id="chked" type="checkbox" checked/><label for="chked">已點擊</label>
<br /><br />
<input id="notChked" type="checkbox" /><label for="notChked">not me brotha!</label>
</form>
</div>
</body>
</html>
//SPAN元素為其父元素的第一個子元素
Ext.query("span:first-child"); // [span.bar]
//A元素為其父元素的最后一個子元素
Ext.query("a:last-child") // [a www.extjs.com, a test.html#]
//SPAN元素為其父元素的第2個子元素(由1開始的個數(shù))
Ext.query("span:nth-child(2)") // [span.bar]
//TR元素為其父元素的奇數(shù)個數(shù)的子元素
Ext.query("tr:nth-child(odd)") // [tr, tr]
//LI元素為其父元素的奇數(shù)個數(shù)的子元素
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]
//返回沒有包含F(xiàn)ORM子元素以外的那些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]
//返回接著會繼續(xù)有TD的那些TD集合。尤其一個地方是,如果使用了colspan屬性的TD便會忽略
Ext.query("td:next(td)") // [td, td]
//返回居前于INPUT元素的那些LABEL元素集合
Ext.query("label:prev(input)") //[label, label]
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函數(shù)返回的其實是一個DOM Node的數(shù)組,而Ext.get的參數(shù)el可以是DOM Node,哈哈,明白了嗎?就是說要實現(xiàn)最靈活的取法,應該用query取到DOM Node然后交給get去變成Element。也就是:
var x=Ext.query(QueryStr);
//我為什么不寫成內(nèi)聯(lián)函數(shù)形式?因為這里的x只能是一個元素,而上面那句的x是一個Array,大家自己轉(zhuǎn)換和處理吧
var y=Ext.get(x);
那么接下來需要介紹QueryStr的格式(其實和jQuery里的selector的格式很像啦),至于獲得Element后可以干些啥,大家自己去看ExtJS文檔里關于Ext.Element的說明,我就不摘過來了。
先給一個html代碼,好做演示說明
復制代碼 代碼如下:
<html>
<body>
<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 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>
<div name="BlueLotus7">BlueLotus7@126.com</div>
</body>
</hmlt>
(1)根據(jù)標記?。?/ 這個查詢會返回有兩個元素的數(shù)組因為查詢選中對整個文檔的所有span標簽。
Ext.query("span");
// 這個查詢會返回有一個元素的數(shù)組因為查詢顧及到了foo這個id。
Ext.query("span", "foo");// 這會返回有一個元素的數(shù)組,內(nèi)容為div標簽下的p標簽
Ext.query("div p");
// 這會返回有兩個元素的數(shù)組,內(nèi)容為div標簽下的span標簽
Ext.query("div span");(2)根據(jù)ID?。?/ 這個查詢會返回包含我們foo div一個元素的數(shù)組!
Ext.query("#foo"); //或者直接Ext.get("foo");(3)根據(jù)class的Name去?。篍xt.query(".foo");// 這個查詢會返回5個元素的數(shù)組。
Ext.query("*[class]"); // 結(jié)果: [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”結(jié)尾的所有元素
Ext.query("*[class$=r]");
//這會得到在class中抽出“a”字符的所有元素
Ext.query("*[class*=a]");//這會得到name等于“BlueLotus7”的所有元素
Ext.query("*[name=BlueLotus7]");
我們換個html代碼:
復制代碼 代碼如下:
<html>
<head>
</head>
<body>
<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 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>
// 獲取所以紅色的元素
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”結(jié)束的元素
Ext.query("*{color$=ow}"); // [a www.extjs.com]
// 獲取所有顏色屬性包含“ow”字符的元素
Ext.query("*{color*=ow}"); // [a www.extjs.com, span.bar]
(5)偽操作符取法換個html:
復制代碼 代碼如下:
<html>
<head>
</head>
<body>
<div id="bar" class="foo" style="color:red; border: 2px dotted red; margin:5px; padding:5px;">
我是一個div ==> 我的id是bar,我的class是foo
<span class="bar" style="color:pink;">這里是span元素,外層的div元素有foo的class屬性</span>
<a target="_blank" style="color:yellow;">設置blank=target的ExtJS鏈接</a>
</div>
<div id="foo" class="bar" style="color:fushia; border: 2px dotted black; margin:5px; padding:5px;">
這里的id是:foo,這里的class是bar
<p>“foo” div包圍下的p元素。</p>
<span class="bar" style="color:brown;">這里是一個span元素,外層是div包圍著,span還有一個bar的class屬性。</span>
<a href="#" style="color:green;">內(nèi)置鏈接</a>
</div>
<div style="border:2px dotted pink; margin:5px; padding:5px;">
<ul>
<li>條目 #1</li>
<li>條目 #2</li>
<li>條目 #3</li>
<li>條目 #4 帶有<a href="#">鏈接</a></li>
</ul>
<table style="border:1px dotted black;">
<tr style="color:pink">
<td>第一行,第一列</td>
<td>第一行,第二列</td>
</tr>
<tr style="color:brown">
<td colspan="2">第二行,已合并單元格!</td>
</tr>
<tr>
<td>第三行,第一列</td>
<td>第三行,第二列</td>
</tr>
</table>
</div>
<div style="border:2px dotted red; margin:5px; padding:5px;">
<form>
<input id="chked" type="checkbox" checked/><label for="chked">已點擊</label>
<br /><br />
<input id="notChked" type="checkbox" /><label for="notChked">not me brotha!</label>
</form>
</div>
</body>
</html>
//SPAN元素為其父元素的第一個子元素
Ext.query("span:first-child"); // [span.bar]
//A元素為其父元素的最后一個子元素
Ext.query("a:last-child") // [a www.extjs.com, a test.html#]
//SPAN元素為其父元素的第2個子元素(由1開始的個數(shù))
Ext.query("span:nth-child(2)") // [span.bar]
//TR元素為其父元素的奇數(shù)個數(shù)的子元素
Ext.query("tr:nth-child(odd)") // [tr, tr]
//LI元素為其父元素的奇數(shù)個數(shù)的子元素
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]
//返回沒有包含F(xiàn)ORM子元素以外的那些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]
//返回接著會繼續(xù)有TD的那些TD集合。尤其一個地方是,如果使用了colspan屬性的TD便會忽略
Ext.query("td:next(td)") // [td, td]
//返回居前于INPUT元素的那些LABEL元素集合
Ext.query("label:prev(input)") //[label, label]
相關文章
jQuery結(jié)合CSS制作漂亮的select下拉菜單
對于我來說,標準的HTML元素(Select)已經(jīng)讓我感到討厭。它不能夠正常的在IE瀏覽器上顯示。還有一點就是他并不僅僅包含簡單的文本。本實例將完全摒棄select元素,通過JQuery和CSS來構(gòu)建DropDown元素。2015-05-05jquery 單行滾動、批量多行滾動、文字圖片翻屏滾動效果代碼
jquery單行滾動、批量多行滾動、文字圖片翻屏滾動效果代碼,需要的朋友可以參考下。2010-05-05Jquery.TreeView結(jié)合ASP.Net和數(shù)據(jù)庫生成菜單導航條
在網(wǎng)上瀏覽了許多關于利用Jquery.TreeView插件生成樹的例子!但是大多數(shù)都沒有結(jié)合數(shù)據(jù)庫來生成樹,很難運用到實際項目中!2010-08-08jQuery使用animate創(chuàng)建動畫用法實例
這篇文章主要介紹了jQuery使用animate創(chuàng)建動畫用法,可實現(xiàn)點擊鏈接文字隱藏及顯示文字的功能,實例分析了jquery中toggle與animate方法的使用技巧,需要的朋友可以參考下2015-08-08