更新:请看下面的代码: div class='parent' div class='first'whatever content/div div class='child1'/div Paragraph 1 br Paragraph 2 div class='child2'/div div class='child3'whatever/div i don't wanna this text div class='last'whate
<div class='parent'> <div class='first'>whatever content</div> <div class='child1'></div> Paragraph 1 <br> Paragraph 2 <div class='child2'></div> <div class='child3'>whatever</div> i don't wanna this text <div class='last'>whatever</div> </div>
我怎样才能得到“第1段第2段”的文字
没有文字“我不想这个文字”
从那个代码?
谢谢.
注意:
我无法编辑/更改HTML代码.
HTML:
<div class="parent"> <div class="first">whatever content</div> <div class="child1"></div> Paragraph 1 <br> Paragraph 2 <div class="child2"></div> <div class="child3">whatever</div> Last Content <div class="last">whatever</div> </div>
JQUERY:
var content = $('.parent').clone().children().remove().end().text(); alert(content);
演示:http://jsfiddle.net/vRbCw/3/
2-如何删除div.child3之前的内容:
JQUERY:
var appendSt = $('.child3').after('code:'), content = $('.parent').clone().children().remove().end().text(), reg = content.replace(/code:([^xyz]+)/, ''); alert(reg);
演示:http://jsfiddle.net/sBSRH/
获取没有子元素的元素文本:http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/