我正在从数据库中加载文本,但我想使用 JavaScript 从中删除 html 链接代码。
假设文本区域现在显示:
<a rel="nofollow" href="http://stackoverflow.com//questions/ask">http://stackoverflow.com//questions/ask</a> - good page
我希望它显示:
http://stackoverflow.com//questions/ask - good page
有没有我可以使用的轻量级的东西可以用于同一文本区域中的多个链接?
最佳答案
灵感来自 this answer ,使用浏览器的 HTML 解析功能来正确完成此操作。
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
jQuery('#textareaid').text(function(index, text){
return strip(text);
});
这是它工作的 JSFiddle:http://jsfiddle.net/Au95R/1/
(编辑为使用更清洁的 JS)
关于javascript - 如何使用 JavaScript 从 textarea 中删除 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635297/