DomQuery v1.1 高级

Tutorial:DomQuery v1.1 Advanced

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: DomQuery Advanced
Author: Bernard Chhun
Published: september 13th 2007
Ext Version: 1.1
Languages: en.pngEnglish

This tutorial will drive you through some advanced DOM manipulation using the results we may get when we use DomQuery.

page_edit.png DomQuery Advanced

page_edit.png Highlight my DOM damn it!

here's an homage clone to what Karl Swedberg did for the JQuery Manual.

It basically highlights some DOM elements based on what button we clicked on. The Ext code is already in the page and you guys will need to give it the appropriate Ext JS files.


Known bugs

  • the 2nd selector doesn't select all even LI's
  • the 8th selector dies on an error


Image:DomQuery_v1_dot_1_Advanced_highlight_my_dom_damnit.png 

None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
None.gif
< html >
None.gif    
< head >
None.gif        
< meta  http-equiv ="Content-Type"  content ="text/html; charset=iso-8859-1" >
None.gif        
< title > Tutorials - DomQuery </ title >
None.gif        
< script  type ="text/javascript"  src ="../js/firebug/firebug.js" ></ script >
ExpandedBlockStart.gifContractedBlock.gif        
< style > dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            .yellow
{dot.gif}{background-color:yellow;}
ExpandedBlockEnd.gif        
</ style >
None.gif    
</ head >
None.gif    
< body  id ="body" >
None.gif        
< script  type ="text/javascript"  src ="../ext/ext-base.js" ></ script >
None.gif        
< script  type ="text/javascript"  src ="../ext/ext-core.js" ></ script >
ExpandedBlockStart.gifContractedBlock.gif        
< script > dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
var domquery = function()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
returndot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                init: 
function()dot.gif{
InBlock.gif                    Ext.select(
"div.dom-traversal-toggles").on("click",
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
function(e, el)dot.gif{
InBlock.gif                            
var id = el.id;
InBlock.gif                            id 
= id.replace("dt-link""");
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
if (id != "")dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
var toggler = function(e)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
var highlight = function(els)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                        
for (var x = 0 ; x < els.length; x ++)dot.gif{
InBlock.gif                                            
var el = Ext.get(els[x]);
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
if (el) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
if (el.hasClass("yellow")) dot.gif{
InBlock.gif                                                    el.removeClass(
"yellow");
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                }
elsedot.gif{
InBlock.gif                                                    el.addClass(
"yellow");
ExpandedSubBlockEnd.gif                                                }
    
ExpandedSubBlockEnd.gif                                            }

ExpandedSubBlockEnd.gif                                        }

InBlock.gif                                        
return els;
ExpandedSubBlockEnd.gif                                    }
;
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
var highlightHidden = function(els)dot.gif{
InBlock.gif                                        
var elmts = highlight(els);
ExpandedSubBlockStart.gifContractedSubBlock.gif                                        
for (var x = 0 ; x < elmts.length; x ++)dot.gif{
InBlock.gif                                            
var el = Ext.get(elmts[x]);
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
if (el) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
if (el.hasClass("yellow"))dot.gif{
InBlock.gif                                                    el.fadeIn();    
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                }
elsedot.gif{
InBlock.gif                                                    el.fadeOut();
InBlock.gif                                                    el.removeClass(
"yellow");
ExpandedSubBlockEnd.gif                                                }
    
ExpandedSubBlockEnd.gif                                            }
    
ExpandedSubBlockEnd.gif                                        }

InBlock.gif 
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
switch(e)dot.gif{
InBlock.gif                                        
case "1":
InBlock.gif                                            highlight(Ext.query(
'li:first'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "2":
InBlock.gif                                            highlight(Ext.query(
'li:even'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "3":
InBlock.gif                                            highlight(Ext.query(
'li'"extdt").splice(0,3));
InBlock.gif                                        
break;
InBlock.gif                                        
case "4":
InBlock.gif                                            highlight(Ext.query(
'li:not(.goofy)'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "5":
InBlock.gif                                            highlight(Ext.query(
'p a[href*=#]'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "6":
InBlock.gif                                            highlight(Ext.query(
'code, li.goofy'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "7":
InBlock.gif                                            highlight(Ext.query(
'ul .goofy > strong'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "8":
InBlock.gif                                            highlight(Ext.query(
'li+li>a[href$=pdf]'"extdt"));
InBlock.gif                                        
break;
InBlock.gif                                        
case "9":
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
if (Ext.query("span{display=none}""extdt").length > 0dot.gif{
InBlock.gif                                                highlightHidden( Ext.query(
"span{display=none}""extdt") );    
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            }
else dot.gif{
InBlock.gif                                                highlightHidden( Ext.query(
"span{display}""extdt"));
ExpandedSubBlockEnd.gif                                            }

InBlock.gif 
InBlock.gif                                        
break;
InBlock.gif                                        
case "10":
InBlock.gif                                            highlight( Ext.query(
"li:nth(4)""extdt") );
InBlock.gif                                        
break;
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockEnd.gif                                }
(id);
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
if (e == "x")dot.gif{
InBlock.gif 
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                    );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
();
InBlock.gif 
InBlock.gif        Ext.onReady(
ExpandedSubBlockStart.gifContractedSubBlock.gif            
function()dot.gif{
InBlock.gif                domquery.init();
ExpandedSubBlockEnd.gif            }

InBlock.gif        );    
ExpandedBlockEnd.gif        
</ script >
None.gif 
None.gif        
< div  style ="border: 1px solid rgb(0, 0, 0); padding: 1em; width: 400px;"  id ="extdt" >
None.gif            
< class ="goofy" >  This is a  < em > paragraph </ em >  of  < strong > text </ strong >  with class=”goofy.” It has an  < title ="http://www.englishrules.com"  class ="external text"  href ="http://www.englishrules.com" > external link </ a > , some  < code > $(code) </ code > , and a  < title =""  href ="#dt-link3_same-page_link" > #dt-link3 same-page link </ a > </ p >
None.gif            
< ul >
None.gif                
< li > list item 1 with dummy link to
None.gif                    
< title ="Silly.pdf"  class ="new"  href ="/action/edit/Silly.pdf" > silly.pdf </ a >
None.gif                
</ li >
None.gif                
< li  class ="goofy" >
None.gif                    
< em > list  < strong > item </ strong >  2 </ em >  with class=” < strong > goofy </ strong >
None.gif                
</ li >
None.gif                
< li  > list item 3
None.gif                    
< span  style ="display:none;" >  SURPRISE! </ span >
None.gif                
</ li >
None.gif                
< li >
None.gif                    
< strong > list item 4 </ strong >  with silly link to
None.gif                    
< title ="Silly.pdf silly.pdf"  class ="new"  href ="/action/edit/Silly.pdf_silly.pdf" > silly.pdf silly.pdf </ a >
None.gif                
</ li >
None.gif            
</ ul >
None.gif        
</ div >
None.gif 
None.gif        
< div  class ="dom-traversal-toggles" >
None.gif            
< ul >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link1"   />   < code > Ext.query('li:first') </ code >  gets the first list item </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link2"   />   < code > Ext.query('li:even') </ code >  gets all odd-numbered list items (because, in javascript, numbering starts with 0, not 1). </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link3" />   < code > Ext.query('li').splice(0,3) </ code >  gets the first 3 list items. “lt” stands for “less than,” and it starts counting at 0, not 1.  </ li >
None.gif 
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link4" />   < code > Ext.query('li:not(.goofy)') </ code >  gets list items 1, 2, and 4, because they’re not “goofy.” </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link5" />   < code > Ext.query('p a[href*=#]') </ code >  gets any links that are inside a paragraph and have an “href” attribute containing “#” anywhere. </ li >
None.gif 
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link6" />   < code > Ext.query('code, li.goofy') </ code >  gets all code elements  < em > and </ em >  any list item with a class of “goofy.” </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link7" />   < code > Ext.query('ul .goofy > strong') </ code >  gets all strong elements that are children of any element with a class of “goofy” as long as the class somewhere inside (i.e. a descendent) of an ordered list. Notice that the word “item” is not highlighted because, even though it’s inside “.goofy,” it’s not a direct child. Only “goofy” is a direct child of “.goofy.” Maybe we should call it “goofy jr.”  </ li >
None.gif 
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link8" />   < code > Ext.query('li + li > a[@href$=pdf]') </ code >  gets all links ending in “pdf” that are children of any list item that has another list item as its previous sibling. It won’t get the first list item’s silly.pdf because that list item has no other list items before it. </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link9" />   < code > Ext.query("span{display=none}") </ code >  gets any span element that is hidden. </ li >
None.gif                
< li >< input  type ="submit"  value ="toggle"  id ="dt-link10" />   < code > Ext.query("li:nth(4)") </ code >  gets the 4th li element </ li >
None.gif 
None.gif            
</ ul >
None.gif        
</ div >
None.gif    
</ body >
None.gif
</ html >
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值