How to remove text inside an element with jQuery?
I have a script that creates a list of items with a structure like this:
<li>
<div>Some stuff</div>
<a href="http://www.mysite.com/">New Item</a>
(1 vote)
</li>
I was wondering 开发者_StackOverflow中文版if there was a way to remove everything outside the <div>
and <a>
tags, in this case the (1 vote) string, with jQuery or regular javascript.
Thanks in advance!
This should work.
$("li").contents().filter(function(){ return this.nodeType != 1; }).remove();
or by specifying text nodes explicitly
$("li").contents().filter(function(){ return this.nodeType == 3; }).remove();
See this fiddle.
$('li').html($(this).children())
You can try this, it should work. Clean and easy.
$('li').not('div,a').text('')
Try this, untested
Edit
$('li').contents().filter(function(){ return !(this.tagName == 'DIV'
|| this.tagName == 'A');}).remove();
精彩评论