2011-05-16 49 views
23

內獲得li元素我有我的網頁上這個HTML:jQuery.each - 一個UL

<div class="phrase"> 
    <ul class="items"> 
     <li class="agap"><ul><li>TEXT1</li></ul></li> 
     <li class="agap"><ul> </ul></li> <!-- empty ul --> 
     <li class="aword">TEXT2</li> 
     .. 
    </ul> 
</div> 

<div class="phrase"> ... </div> 

我想獲得每個「短語」都在「項目」,在文本變量中的元素,像這樣:

var string = "TEXT1 - BLANK - TEXT2"; 

目前,我有這個JavaScript代碼:

<script> 
$(function() { 
    $('.phrase .items').each(function(){ 
     var myText = ""; 

     // At this point I need to loop all li items and get the text inside 
     // depending on the class attribute 

     alert(myText); 

    }); 
}; 
</script> 

我如何可以遍歷所有<li>.items

我在嘗試不同的方法,但沒有得到好的結果。

回答

43

首先,我認爲你需要修復您的名單,作爲<ul>的第一個節點必須是一個<li>stackoverflow ref)。一旦設置你可以這樣做:

// note this array has outer scope 
var phrases = []; 

$('.phrase').each(function(){ 
     // this is inner scope, in reference to the .phrase element 
     var phrase = ''; 
     $(this).find('li').each(function(){ 
      // cache jquery var 
      var current = $(this); 
      // check if our current li has children (sub elements) 
      // if it does, skip it 
      // ps, you can work with this by seeing if the first child 
      // is a UL with blank inside and odd your custom BLANK text 
      if(current.children().size() > 0) {return true;} 
      // add current text to our current phrase 
      phrase += current.text(); 
     }); 
     // now that our current phrase is completely build we add it to our outer array 
     phrases.push(phrase); 
    }); 
    // note the comma in the alert shows separate phrases 
    alert(phrases); 

工作jsfiddle

有一件事是,如果你得到.text()上層li你會得到它的所有子級文本。

保持一個數組將允許提取許多多個短語。


編輯:

這應該沒有LI與空UL更好地工作:

// outer scope 
var phrases = []; 

$('.phrase').each(function(){ 
    // inner scope 
    var phrase = ''; 
    $(this).find('li').each(function(){ 
     // cache jquery object 
     var current = $(this); 
     // check for sub levels 
     if(current.children().size() > 0) { 
      // check is sublevel is just empty UL 
      var emptyULtest = current.children().eq(0); 
      if(emptyULtest.is('ul') && $.trim(emptyULtest.text())==""){ 
       phrase += ' -BLANK- '; //custom blank text 
       return true; 
      } else { 
      // else it is an actual sublevel with li's 
      return true; 
      } 
     } 
     // if it gets to here it is actual li 
     phrase += current.text(); 
    }); 
    phrases.push(phrase); 
}); 
// note the comma to separate multiple phrases 
alert(phrases); 
+0

這是最好的解決方案。良好的工作:) – MarioRicalde 2011-05-16 12:51:44

+0

這是爲我工作,但我需要知道何時ul沒有李,所以,在這種情況下,我應該得到「空白」作爲文本。也許我的帖子根本不清楚。 我不明白這一行: if(current.children()。size()> 0){return true;} 感謝您的回答。 – rusben 2011-05-16 13:08:56

+0

那麼,這在技術上是不合法的標記。但是你可以用它來處理它。我會用一些澄清的評論更新我的答案 - 現在已更新! – WSkid 2011-05-16 13:14:19

7
$(function() { 
    $('.phrase .items').each(function(i, items_list){ 
     var myText = ""; 

     $(items_list).find('li').each(function(j, li){ 
      alert(li.text()); 
     }) 

     alert(myText); 

    }); 
};