仅对真实角色按下按键?
简单的问题: 我有带有内部标签的输入字段!就像本网站右上角的搜索框一样。当我开始输入其中的标签时,它被隐藏了。
出于可访问性的原因,我将真正的标签绝对定位在实际输入字段的后面。因此,我只是向输入字段添加一个类 fill
,这样背景颜色就不再透明了。
inputs.keydown(function (e) {
$(this).addClass(fill);
});
除了一点小缺陷之外,一切都运行良好。 每当我聚焦输入字段并按下“Shift”、“Ctrl”或“CMD”等键时,标签就会消失。然而这还没有输入!
知道该怎么做吗?
inputs.keydown(function (e) {
switch (e.keyCode) {
case 13: // Enter
case 16: // Shift
case 17: // Ctrl
case 18: // Alt
case 19: // Pause/Break
case 20: // Caps Lock
case 27: // Escape
case 35: // End
case 36: // Home
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
// Mac CMD Key
case 91: // Safari, Chrome
case 93: // Safari, Chrome
case 224: // Firefox
break;
}
$(this).addClass(fill);
});
这些将是我不想模糊标签的所有键码!当输入真实字符时,应该添加填充类。 顺便提一句。是否有更好的语法来编写所有情况下的 switch 语句?
simple question:
I have input fields with inside-labels! Just like the search-box on this site in the upper right corner. When I start typing the label inside it is hidden.
For accessability reasons I have real tags absolutely positioned behind the actual input field. Therefore I just add a class fill
to the inputfield so the background color is no longer transparent.
inputs.keydown(function (e) {
$(this).addClass(fill);
});
Everything works fine except for a little flaw.
Whenever I focus the input field and hit a key like "Shift" "Ctrl" or "CMD" the label disappears. However this is no input yet!
Any idea how to do so?
inputs.keydown(function (e) {
switch (e.keyCode) {
case 13: // Enter
case 16: // Shift
case 17: // Ctrl
case 18: // Alt
case 19: // Pause/Break
case 20: // Caps Lock
case 27: // Escape
case 35: // End
case 36: // Home
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
// Mac CMD Key
case 91: // Safari, Chrome
case 93: // Safari, Chrome
case 224: // Firefox
break;
}
$(this).addClass(fill);
});
These would be all the keycodes where I don't want to blur the label! Just when typing real characters the fill-class should be added.
Btw. is there a better syntax to write those switch statements with all the cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要处理字符输入,请使用
keypress()
。它已经忽略了大多数您想要避免的键。它确实会在Enter
处触发,但您可以按照此处已建议的相同方式轻松解释这一点。Use
keypress()
instead if you're dealing with character inputs. It already takes care of ignoring most of the keys you want to avoid. It does get triggered atEnter
, but you can easily account for that in the same manner already proposed here.这只会在没有遇到任何情况时执行 addClass:
但是由于您使用的是 jquery 并且想要一个较小的解决方案,请尝试以下操作:
This would only execute the addClass when no case is hit:
But since you are using jquery and want a smaller solution try this:
您可以做的是获取按键前后的文本长度,看看是否有差异。如果有,则这是一个真正的字符或粘贴事件,在这种情况下您需要应用背景颜色。
即:
更好的是,使用 jQuery textchange 事件插件。 http://www.zurb.com/playground/jquery-text-change -自定义事件
What you can do is get the text length before and after the key press and see if there is a difference. If there is, it was a real char or a paste event, in which case you need to apply the background color.
i.e. Something like:
Still better, use the jQuery textchange event plugin. http://www.zurb.com/playground/jquery-text-change-custom-event