2010-10-08 103 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

相关问题