每当我在 textarea 中粘贴文本时,它应该去掉 <、>、@ 等字符。我尝试 在 JQuery 中
$('input').on('paste', function () { var element = this; setTimeout(function () { var text = $(element).val(); // do something with text }, 100); });
$('input').on('paste', function () {
var element = this;
setTimeout(function () {
var text = $(element).val();
text.replace('<', '') );
}, 100); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="test" name="test" style="height:300px; width:400px"></textarea>
最佳答案
您正在使用 input
element selector它只匹配标记名为 input
的元素喜欢<input type="..." />
, 而不是 textarea
所以
$('textarea').on('paste', function() {
var $el = $(this);
setTimeout(function() {
$el.val(function(i, val) {
return val.replace(/[<>@]/g, '')
})
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea id="test" name="test" style="height:300px; width:400px"></textarea>
关于javascript - 粘贴文本时去除textarea中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359238/