每当我在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>