-2

从标签中去除字符串的最快(性能)方法是什么,我尝试过的大多数解决方案都使用正则表达式,但不会为属性内的标签生成正确的值(是的,我知道这是错误的),示例测试用例:

 var str = "<div data-content='yo! press this: <br/> <button type=\"button\"><i class=\"glyphicon glyphicon-disk\"></i> Save</button>' data-title='<div>this one for tooltips <div>seriously</div></div>'> this is the real content<div> with another nested</div></div>"

这应该导致:

 this is the real content with another nested
4

1 回答 1

3

我认为使用 innerText 应该很快:

var str = "<div data-content='yo! press this: <br/> <button type='button'><i class=\"glyphicon glyphicon-disk\"></i></button>' data-title='<div>this one for tooltips</div>'> this is the real content<div> with another nested</div></div>"; 
var el = document.createElement('div');
el.innerHTML = str;
var text = el.textContent || el.innerText;
alert(text);
于 2013-11-08T09:22:06.457 回答