Javascript 你如何在 textarea 输入中去除 html 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12022614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do you strip html tags in textarea input
提问by sgerbhctim
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).
我已经建立了一个小的 Web 应用程序,您可以在其中向页面添加便利贴。这只是一个帮助更好地学习 JavaScript/jQuery 的项目,它似乎是一个合适的项目(我是 JS/jQuery 的新手)。
This is the fiddle: http://jsfiddle.net/mitchbregs/mWaPz/
这是小提琴:http: //jsfiddle.net/mitchbregs/mWaPz/
I found this website: http://www.hscripts.com/scripts/JavaScript/remove-html-tag.php
我找到了这个网站: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, "") }
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.
问题:我需要它,以便当有人将文本输入到 textarea 时,如果他们输入类似 " <b>Hi</b>
" 的内容,它会<b>
从输入中去除 " " 标签并只留下文本。
If someone could help me out this it would be great.. Thanks!
如果有人能帮我解决这个问题,那就太好了..谢谢!
回答by Jo?o Silva
回答by Eric
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).
你有三个问题:一个是方法没有正确返回(这是编写它的人的错)。接下来是您在 中定义方法$(document).ready()
,因此无法找到它(至少在 Chrome 中)。第三,您没有以正确的方式调用它(onSubmit
此处永远不会使用)。
I've used @Joao's method (so you should upvote his answer) and used this method, moved outside of the ready()
function:
我使用了@Joao 的方法(所以你应该赞成他的回答)并使用这个方法,移到ready()
函数之外:
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/
这是工作小提琴:http: //jsfiddle.net/ee3MH/
回答by Petar Sabev
function stripHTML(text){
var regex = /(<([^>]+)>)/ig;
return text.replace(regex, "");
}
// USE: var str = stripHTML('<b>test</b>');
回答by Henrik Karlsson
You could use the jquery .text function, though it will rather escape the html than strip it:
您可以使用 jquery .text 函数,但它宁愿转义 html 而不是剥离它:
$(".myTxtBox").text("<b>this text will be displayed, even the b tag.");
回答by adeneo
回答by saluce
No one successfully answered this question in accordance with the OP's request, in that the inner tags would be completely removed from the input, not just the tags themselves; so, here's an answer that will do precisely that (Fiddle):
没有人按照 OP 的要求成功回答了这个问题,因为内部标签将从输入中完全删除,而不仅仅是标签本身;所以,这是一个可以做到这一点的答案(小提琴):
var input = "This is <b>not</b>a successful test";
var output = $("<div/>").html(input).children().remove().end().text()