如何在textarea输入中去除html标签 [英] how do you strip html tags in textarea input

查看:954
本文介绍了如何在textarea输入中去除html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个小的web应用程序,您在其中添加post-it到页面。这只是一个项目,以帮助更好地学习JavaScript / jQuery,它似乎是一个适当的项目(我是新的JS / jQuery)。



这是小提琴: a href =http://jsfiddle.net/mitchbregs/mWaPz/> http://jsfiddle.net/mitchbregs/mWaPz/



我发现此网站: http://www.hscripts.com/scripts/JavaScript/remove-html- tag.php



我实现的代码如下:


  function stripHTML(){
var re = /(<([^>] +)>)/ gi;
for(i = 0; i< arguments.length; i ++)
arguments [i] .value = arguments [i] .value.replace(re,)
}


它仍无法工作。



问题:我需要它,以便当有人向textarea中输入文本时,如果他们输入这样的< b> Hi< / b> ,它会从输入中剥离< b> 标签,只留下文本。



解决方案

您有三个问题:一个是方法没有正确返回(这是谁写的错的)。接下来是你在 $(document).ready()中定义方法,因此无法找到(至少在Chrome中)。第三,你不是以正确的方式调用它( onSubmit 将永远不会在这里使用)。



使用@ Joao的方法(所以你应该upvote他的答案),并使用这种方法,移出 ready()函数:

  function stripHTML(str){
var strippedText = $(< div />)。html(str).text
return strippedText;
}

也称为:

  var post = stripHTML($(。text_input)。val())

这里是工作小提琴: http://jsfiddle.net/ee3MH/ p>

I have set up a little web application where you add post-its to a page. It's just a project to help better learn JavaScript/jQuery and it seemed like an appropriate project (I'm new to JS/jQuery).

This is the fiddle: http://jsfiddle.net/mitchbregs/mWaPz/

I found this website: http://www.hscripts.com/scripts/JavaScript/remove-html-tag.php

I implemented the code as so:

function stripHTML(){
var re = /(<([^>]+)>)/gi;
for (i=0; i < arguments.length; i++)
arguments[i].value=arguments[i].value.replace(re, "")
}

and it still didnt work.

The problem: I need it so that when somebody inputs text into the textarea, if they input something like this "<b>Hi</b>" it would strip the "<b>" tag from the input and leave just the text.

If someone could help me out this it would be great.. Thanks!

解决方案

You have three issues: One is that the method is not returning properly (that's the fault of whoever wrote it). Next is that you are defining the method within $(document).ready(), so it is not able to be found (at least in Chrome). Third, you are not calling it the right way (onSubmit will never be used here).

I've used @Joao's method (so you should upvote his answer) and used this method, moved outside of the ready() function:

function stripHTML(str){
  var strippedText = $("<div/>").html(str).text();
  return strippedText;
}​

And also called it here:

var post = stripHTML($(".text_input").val())

Here is the working fiddle: http://jsfiddle.net/ee3MH/

这篇关于如何在textarea输入中去除html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆