如何修剪输入的值而不阻塞空格字母的输入?
例如,如果我撰写或抄袭了副本" hello world "
,则应将其更正为"hello world"
到目前为止,我拥有的脚本可以正确修剪粘贴的输入,但是如果我手动编写它,它将阻塞空格,以便输出为"helloworld"
。
$(function(){
$('.trim').bind('input', function(){
$(this).val(function(_, v){
return v.trim();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="trim" type="text">
尝试编写blur
事件,它将删除当前文本开头和结尾的空格:
$(function(){
$('.trim').bind('blur', function(){
$(this).val(function(_, v){
return v.trim();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="trim" type="text">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句