仅对真实角色按下按键?

发布于 2024-12-25 07:36:51 字数 1109 浏览 4 评论 0原文

简单的问题: 我有带有内部标签的输入字段!就像本网站右上角的搜索框一样。当我开始输入其中的标签时,它被隐藏了。

出于可访问性的原因,我将真正的标签绝对定位在实际输入字段的后面。因此,我只是向输入字段添加一个类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天白云 2025-01-01 07:36:51

如果您要处理字符输入,请使用 keypress() 。它已经忽略了大多数您想要避免的键。它确实会在 Enter 处触发,但您可以按照此处已建议的相同方式轻松解释这一点。

$("input").keypress(function (e) {
    if( e.which == 13 ){ return false; }
    $(this).addClass(fill);
}

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 at Enter, but you can easily account for that in the same manner already proposed here.

$("input").keypress(function (e) {
    if( e.which == 13 ){ return false; }
    $(this).addClass(fill);
}
单挑你×的.吻 2025-01-01 07:36:51

这只会在没有遇到任何情况时执行 addClass:

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;
        default:
          $(this).addClass(fill);
        break;
    }
});

但是由于您使用的是 jquery 并且想要一个较小的解决方案,请尝试以下操作:

inputs.keydown(function (e) {
  if($.inArray(e.keyCode,[13,16,17,18,19,20,27,35,36,37,38,39,40,91,93,224])) return;
  $(this).addClass(fill);
});

This would only execute the addClass when no case is hit:

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;
        default:
          $(this).addClass(fill);
        break;
    }
});

But since you are using jquery and want a smaller solution try this:

inputs.keydown(function (e) {
  if($.inArray(e.keyCode,[13,16,17,18,19,20,27,35,36,37,38,39,40,91,93,224])) return;
  $(this).addClass(fill);
});
扮仙女 2025-01-01 07:36:51

您可以做的是获取按键前后的文本长度,看看是否有差异。如果有,则这是一个真正的字符或粘贴事件,在这种情况下您需要应用背景颜色。

即:

var dataLength = 0;

// use keyup instead of keydown to ensure that we catch the event after the data has been entered
inputs.keyup(function (e) { 
   if($(this).val().length != dataLength) $(this).addClass(fill);

   ... 

   dataLength = $(this).val().length;

   // re-apply placeholder text if no text
   if(dataLength == 0) $(this).removeClass(fill);
}

更好的是,使用 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:

var dataLength = 0;

// use keyup instead of keydown to ensure that we catch the event after the data has been entered
inputs.keyup(function (e) { 
   if($(this).val().length != dataLength) $(this).addClass(fill);

   ... 

   dataLength = $(this).val().length;

   // re-apply placeholder text if no text
   if(dataLength == 0) $(this).removeClass(fill);
}

Still better, use the jQuery textchange event plugin. http://www.zurb.com/playground/jquery-text-change-custom-event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文