2010-10-08 53 views
3

返回HTML如果我在#parent點擊我要回裏面的文字和回報嵌套
層內的文本(#child-parent#childjQuery的:從DIV,而不是孩子

<div id='parent'> 
    this text is for parent 

    <div id='child-parent'> 
    this text if for child-parent 

     <div id='child'> 
     and this text is for child. 
     </div> 

    </div> 

</div> 


這樣的:
$('#parent').html() = "this text is for parent"

,而不是這樣的:
$('#parent').html() = "this text is for parent this text is for child-parent and this text is for child"

回答

4

你可以抓住這樣說:

$('#parent').clone().children().remove().end().html() 

You can test it out here,你可能要在那裏$.trim()呼叫不過,like this

另一種方法是循環儘管文本節點,這樣的:

$('#parent').contents().filter(function() { return this.nodeType == 3; }).text() 

You can test that herethe trimmed version here

+0

哇,非常感謝你! – Zebra 2010-10-08 13:46:33

+0

非常感謝,我發現了三種新的jQuery方法的存在:) – 2010-10-08 13:56:38

相關問題