• <small id='O3dHp'></small><noframes id='O3dHp'>

      <tfoot id='O3dHp'></tfoot>

      1. <legend id='O3dHp'><style id='O3dHp'><dir id='O3dHp'><q id='O3dHp'></q></dir></style></legend>
      2. <i id='O3dHp'><tr id='O3dHp'><dt id='O3dHp'><q id='O3dHp'><span id='O3dHp'><b id='O3dHp'><form id='O3dHp'><ins id='O3dHp'></ins><ul id='O3dHp'></ul><sub id='O3dHp'></sub></form><legend id='O3dHp'></legend><bdo id='O3dHp'><pre id='O3dHp'><center id='O3dHp'></center></pre></bdo></b><th id='O3dHp'></th></span></q></dt></tr></i><div id='O3dHp'><tfoot id='O3dHp'></tfoot><dl id='O3dHp'><fieldset id='O3dHp'></fieldset></dl></div>
          <bdo id='O3dHp'></bdo><ul id='O3dHp'></ul>

      3. 如何在 keydown 上使用 Javascript 模拟悬停?

        How do I simulate hover with Javascript on keydown?(如何在 keydown 上使用 Javascript 模拟悬停?)

          <small id='ZFxCm'></small><noframes id='ZFxCm'>

            <bdo id='ZFxCm'></bdo><ul id='ZFxCm'></ul>

              <tbody id='ZFxCm'></tbody>
            <i id='ZFxCm'><tr id='ZFxCm'><dt id='ZFxCm'><q id='ZFxCm'><span id='ZFxCm'><b id='ZFxCm'><form id='ZFxCm'><ins id='ZFxCm'></ins><ul id='ZFxCm'></ul><sub id='ZFxCm'></sub></form><legend id='ZFxCm'></legend><bdo id='ZFxCm'><pre id='ZFxCm'><center id='ZFxCm'></center></pre></bdo></b><th id='ZFxCm'></th></span></q></dt></tr></i><div id='ZFxCm'><tfoot id='ZFxCm'></tfoot><dl id='ZFxCm'><fieldset id='ZFxCm'></fieldset></dl></div>
            <tfoot id='ZFxCm'></tfoot>

              <legend id='ZFxCm'><style id='ZFxCm'><dir id='ZFxCm'><q id='ZFxCm'></q></dir></style></legend>

                  本文介绍了如何在 keydown 上使用 Javascript 模拟悬停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  首先,我想只使用原生 JavaScript 来完成这项任务.

                  First of, I'd like to use only native JavaScript to complete this task.

                  假设我要制作一个自定义下拉菜单,HTML 代码看起来像这样.

                  Let's say I am to make a custom dropdown, and the HTML code looks kind of like this.

                  <div class="dropdown">
                    <span class="dropdown-label" style="display:block">Select a thing</span>
                    <ul class="dropdownItemContainer">
                      <li>Item 1</li>
                      <li>Item 2</li>
                      <li>Item 3</li>
                      <li>Item 4</li>
                      <li>Item 5</li>
                      <li>Item 6</li>
                    </ul>
                  </div>
                  

                  在 CSS 文件中,我有类似的东西:

                  In the CSS file I have something close to this:

                  ul.dropdownItemContainer li:hover {
                    background-color: #FF0000;
                  }
                  

                  是的,确实没有下拉行为,但实际上这不是讨论的重点.问题是我想不出一种体面的方法来为此下拉菜单启用键盘控制.期望的结果如下:我按下向下键,第一个选项被突出显示;我再次按下它,第二个选项被突出显示,依此类推.

                  Yeah, there's really no dropdownish behavior, but it's not the point of discussion actually. The problem is that I couldn't think of a decent way to enable keyboard control for this dropdown. The desired outcome is the following: I press the down key, and the first option is highlighted; I press it again, and the second option is highlighted and so on.

                  此时我看到的唯一选择(刚开始学习 JS)是获取 ul 的所有子项,将它们粘贴到一个数组中,并通过每当按下向下键时,JS 都会以正确的方式调用.

                  The only option that I see at this point (just started studying JS) is to fetch all of the ul's children, stick'em into an array and assign the tags a background color through JS methods in a proper way whenever the down key is pressed.

                  另一方面,我仍然有鼠标控制的 CSS 中描述的 :hover 行为.有模拟悬停的聪明方法吗?

                  On the other hand, I still have the :hover behavior described in the CSS for mouse countrol. Is there a smart way of simulating hovers?

                  推荐答案

                  我会在你的 li 元素上简单地分配一个类,并使用 keydown 处理程序来引导它.以下代码并不完整,而是为您提供一些可以使用的东西.

                  I would go with a simple assignment of a class on your li-elements and steer it with a keydown handler. The following code is not meant to be complete but give you something you can work with.

                  var active = document.querySelector(".hover") || document.querySelector(".dropdownItemContainer li");
                  
                  document.addEventListener("keydown",handler);
                  document.addEventListener("mouseover",handler);
                  
                  function handler(e){
                      console.log(e.which);
                          active.classList.remove("hover");
                      if (e.which == 40){
                          active = active.nextElementSibling || active;
                      }else if (e.which == 38){      
                          active = active.previousElementSibling || active;
                      }else{
                          active = e.target;
                      }
                          active.classList.add("hover");
                  }
                  

                  您可以在此处查看工作示例

                  You can see a working example here

                  这篇关于如何在 keydown 上使用 Javascript 模拟悬停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Move link image 5px up on hover(悬停时将链接图像向上移动 5px)
                  How do I inspect CSS pseudo classes with firebug?(如何使用 firebug 检查 CSS 伪类?)
                  Why doesn#39;t CSS hover work on table rows when the cells inside the rows have class names?(当行内的单元格具有类名时,为什么 CSS 悬停在表格行上不起作用?)
                  How to apply a CSS class on hover to dynamically generated submit buttons?(如何在悬停时将 CSS 类应用于动态生成的提交按钮?)
                  Differences between CSS3 :hover and :focus?(CSS3 :hover 和 :focus 的区别?)
                  div hover background-color change?(div 悬停背景颜色变化?)

                  <small id='UPAji'></small><noframes id='UPAji'>

                    <bdo id='UPAji'></bdo><ul id='UPAji'></ul>
                    1. <tfoot id='UPAji'></tfoot>

                            <tbody id='UPAji'></tbody>
                        1. <legend id='UPAji'><style id='UPAji'><dir id='UPAji'><q id='UPAji'></q></dir></style></legend>

                          <i id='UPAji'><tr id='UPAji'><dt id='UPAji'><q id='UPAji'><span id='UPAji'><b id='UPAji'><form id='UPAji'><ins id='UPAji'></ins><ul id='UPAji'></ul><sub id='UPAji'></sub></form><legend id='UPAji'></legend><bdo id='UPAji'><pre id='UPAji'><center id='UPAji'></center></pre></bdo></b><th id='UPAji'></th></span></q></dt></tr></i><div id='UPAji'><tfoot id='UPAji'></tfoot><dl id='UPAji'><fieldset id='UPAji'></fieldset></dl></div>