jQuery getting a text but no html [duplicate]
开发者_Python百科Possible Duplicate:
Getting the contents of an element WITHOUT its children
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.
精彩评论