2012-10-03 47 views

回答

0

您可以使用.clone()的是,像這樣:

b = $('div').clone(); 
b.children('span').remove(); 
alert(b.text()); // alerts "text" 

使用.clone()我們可以使DIV的拷貝,刪除時長,然後再拿到短信,都不會影響原始DOM元素顯示在頁面上。

0
var theText = ""; 
$('div').contents().each(function() { 
    if(this.nodeType === 3) theText += $(this).text(); 
}); 
+0

該方法是['.contents()'](http://api.jquery.com/contents)。否則,這個工程! – nbrooks

1

試試這個

var myText = $('#dd').clone()    
      .children() //select all the children 
      .remove() //remove all the children 
      .end() //again go back to selected element 
      .text(); //get the text of element 
+0

Upvoted,我喜歡你的.end()快捷方式。 – Nelson