admin管理员组

文章数量:1278947

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="//questions/ask">//questions/ask</a> - good page 

and I want it to display:

//questions/ask - good page

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

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.//questions/ask">http://stackoverflow.//questions/ask</a> - good page 

and I want it to display:

http://stackoverflow.//questions/ask - good page

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

Share Improve this question edited May 17, 2013 at 22:02 Turadg 7,6912 gold badges50 silver badges49 bronze badges asked Jan 8, 2011 at 17:58 MatBanikMatBanik 26.9k40 gold badges118 silver badges178 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

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/Au95R/1/

(Edited to use cleaner JS)

You could use strip_tag() like in PHP: http://phpjs/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

本文标签: jqueryHow to remove HTML tags from textarea with JavaScriptStack Overflow