Javascript 如何使用jQuery删除元素内的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6690445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove text inside an element with jQuery?
提问by Javier Villanueva
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 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.
我想知道是否有办法使用 jQuery 或常规 javascript删除<div>
和<a>
标签之外的所有内容,在这种情况下是(1 票)字符串。
Thanks in advance!
提前致谢!
回答by DanielB
回答by hungryMind
$('li').not('div,a').text('')
Try this, untested
试试这个,未经测试
Edit
编辑
$('li').contents().filter(function(){ return !(this.tagName == 'DIV'
|| this.tagName == 'A');}).remove();
回答by pjamfaro
$('li').html($(this).children())
You can try this, it should work. Clean and easy.
你可以试试这个,它应该有效。干净又方便。