从输入jQuery中删除HTML [英] remove HTML from input jquery
本文介绍了从输入jQuery中删除HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我从文本输入中获取.val(),如何确保其中没有HTML而只有纯文本?
If i get the .val() from a text input, how can I make sure there is no HTML in there and just plain text?
我正在编写一个小型聊天程序,但我不希望用户能够输入HTML.
I am writing a small chat program but I dont want users to be able to enter HTML.
推荐答案
您可以通过获取他们输入的HTML的textContent来做到这一点:
You can do it by getting the textContent of the HTML they enter:
// Get the value of the input
var inputText = $('#my-input').val();
// Store only the text and no HTML elements.
inputText = $(inputText)[0].textContent;
这里是一个例子:
http://jsfiddle.net/thom801/5A3sQ/1/
这篇关于从输入jQuery中删除HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文