[英]Delete everything between <> in text in jQuery
我想为我的文本区域创建一个“清除”按钮,该按钮将删除每个<>以及它们之间的所有内容。
所以可以说我有:
<div id="markred">Hello World! My name is <b>John!</b></div>
在“清理”功能之后,我希望它是:
Hello World! My name is John!
我真的不知道该怎么做,尝试了一些正则表达式,然后意识到我真的不知道如何编写像样的正则表达式...
任何帮助将非常感激!
怎么样:
$('#markred').text('');
我想为我的文本区域设置一个“清除”按钮
因此,假设
<div id="markred">Hello World! My name is <b>John!</b></div>
实际上是textarea的文本内容,您可以首先解析内容,然后使用DOM操作过滤掉并保留所有文本节点。 例如:
$('#textarea').val(function (index, value) {
return $($.parseHTML(value))
.find('*') // find all element nodes
.addBack() // include root node(s)
.contents() // include all text nodes
.map(function () { // filter and get text node value
return this.nodeType === 3 ? this.nodeValue : null;
}).get().join(''); // concatenate text values
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.