【发布时间】:2013-06-18 13:06:21
【问题描述】:
如何获取带有 html 标签的文本,但只能从主 DIV 获取?不形成其他 DIVS。
这里是例子,但是有一些问题,因为文本没有html标签br />
HTML
<div id='parent'>
this text is <br />for parent
<div id='child-parent'>
this text if for child-parent
<div id='child'>
and this text is for child.
</div>
</div>
</div>
jQuery
alert($('#parent').clone().children().remove().end().html());
【问题讨论】:
-
$.text()返回不带标签的内容字符串。 -
@Broxzier -- 他想要那些标签。
-
你能解释一下你期望从这个例子中返回什么吗?
-
但是我想返回带有标签的字符串。
-
alert($('#parent').find(':not(br)').remove().end().html());