我在这里看到一些类似的问题,但他们真的不回答我
所以我这样做:(在文档准备功能内)
$("#dest").focusin(function() {
$("#dest").val($.trim($("#dest").val()));
});
想法就是当用户将焦点放在一个名为#dest的输入上时,修剪它上面的所有空格字符(以前使用focusout添加视觉舒适度).
现在没有任何事情发生. 🙁
希望有人可以帮我一下
谢谢!
这是电脑有关问题吗?
我已经测试了所有由评论者提供的代码,没有工作.我在OSX(Snow Leopard)10.6.8下使用Firefox和Safari,还有10.8.2(Lion)下的Safari,我得到了相同的结果… OSX问题? – 一切正常,检查我的最后一次编辑!
最终编辑和解决方案感谢Phil Klein
我的问题是使用错误的jQuery的trim()函数…根据trim()documentation它执行以下操作:
The $.trim() function removes all newlines, spaces (including
non-breaking spaces), and tabs from the beginning and end of the
supplied string. If these whitespace characters occur in the middle of
the string, they are preserved.
昨天我没有阅读从提供的字符串的开头和结尾说的最后一部分 – 对不起所有人. 🙁
幸运的是,在上面的绘图之后,@Phil Klein明白了我的错误,并帮助我解决了一个问题:
$(function() {
$("#dest").on("focus", function() {
var dest = $(this);
dest.val(dest.val().split(" ").join(""));
});
});
感谢@Phil Klein以及所有帮助我的人,