2012-11-12 126 views
2

我有以下的div包含表和它的數據從數據庫嵌套的標籤名不起作用

<div id="content"> 
<table> 
<tbody> 
    <tr> 
     <th class="header" colspan="2">Food items include:</th> 
    </tr> 
    <tr> 
     <td id="15" class="fruits">Papaya+salt</td> 
     <td><p>This includes papaya and salt</p></td> 
    </tr> 
    <tr> 
     <td class="meat">Baked chicken</td> 
     <td><p>This includes a chicken thide and kethup</p></td> 
    </tr> 
    <tr> 
     <td id="1" class="Juices">Strawberry Sting</td> 
     <td><p>Sugar, color and water</p></td> 
    </tr> 
<table> 
</div> 

該表是在page.aspx定義查詢

,這裏是用我的代碼,該表數據按字母順序排序

OldFunc = window.onload; 
window.onload = OnLoad; 

function OnLoad(){ 
    try{ 
     var pathName = window.location.pathname.toLowerCase(); 
     if(pathName=="/Resources/Glossary.aspx") { 
       sort_it(); 
      } 
     OldFunc(); 
    } 
    catch(e) { 
    } 
} 

function TermDefinition(def_term,def_desc) 
{  
    this.def_term=def_term; 
    this.def_desc=def_desc; 
}  

function sort_it() 
{ 
    var gloss_list=document.getElementsByTagName('td');  
    var desc_list=document.getElementsByTagName('td p');  
    var gloss_defs=[]; 
    var list_length=gloss_list.length;  
    for(var i=0;i<list_length;i++)  
    {  
     gloss_defs[i]=new TermDefinition(gloss_list[i].firstChild.nodeValue,desc_list[i].firstChild.nodeValue);  
    }  
    gloss_defs.sort(function(a, b){ 
     var termA=a.def_term.toLocaleUpperCase(); 
     var termB=b.def_term.toLocaleUpperCase(); 
     if (termA < termB) 
     return -1; 
     if (termA > termB) 
     return 1; 
     return 0; 
    })  
    for(var i=0;i<gloss_defs.length;i++) 
    { 
     gloss_list[i].firstChild.nodeValue=gloss_defs[i].def_term; 
     desc_list[i].firstChild.nodeValue=gloss_defs[i].def_desc; 
    } 
} 

請注視的兩個的getElementsByTagName,我覺得我濫用它的內容因爲沒有上鄰完成本安輸出。

回答

4

無效

desc_list=document.getElementsByTagName('td p'); 

不能在CSS選擇器傳遞給函數,只喜歡div \ spaninput等」標籤名稱。

您可能需要使用:

desc_list = $('td p'); 

由於您使用jQuery標籤的問題,或document.querySelectorAll香草JS:

desc_list = document.querySelectorAll('td p');