开发者

jQuery getting a text but no html [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Getting the contents of an element WITHOUT its children

开发者_Python百科

How can I get foobar out of the following HTML with jquery/javascript, without using regular expressions?

<div id="somediv">
foobar
<span></span>
</div>


Just use .text().

$("#somediv").text();

Or, if the span has content which you want to ignore, use:

$("#somediv").contents().first().text();


If you just want the text from the first child node, use

$("#somediv").contents().eq(0).text()


Try this:

var text = document.getElementById("somediv").getElementsByTagName("span")[0].innerHTML;


You can do the following:

$('#somediv').text().trim();

The trim will get rid of the extra whitespace from the carriage returns and leave you with just foobar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜