开发者

How to remove HTML tags from textarea with JavaScript

I'm loading text from database but I'd like to remove html link code from it with JavaScript.

So lets say the textarea right now displays:

<a rel="nofollow" href="http://stackoverflow.com//questions/ask">http://stackoverflow.com//questions/ask</a> - good page 

and I want it to display:

http://stackoverflow.com//questions/ask - good page
开发者_开发百科

Is there something lightweight I could use that would work for multiple links in the same textarea?


Inspired by this answer, use the browser's HTML parsing abilities to get this done right.

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);
});

Here's the JSFiddle of it working: http://jsfiddle.net/Au95R/1/

(Edited to use cleaner JS)


You could use strip_tag() like in PHP: http://phpjs.org/functions/strip_tags:535

textareacontent = strip_tags(textareacontent, "<b><i>"); // remove all HTML except <b> and <i>.


you can do this using regular expressions. here is a question on stack overflow itself and the answer explains it well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜