【问题标题】:in Firefox, click on absolute element inside focusable element does not focus focusable element unless it has a CSS position在 Firefox 中,单击可聚焦元素内的绝对元素不会聚焦可聚焦元素,除非它具有 CSS 位置
【发布时间】:2015-04-30 14:55:39
【问题描述】:

上下文:可聚焦元素内的绝对元素。

在 Firefox 36 中,如果可聚焦元素没有 CSS 位置(相对、固定或绝对),则单击内部元素不会将焦点设置到可聚焦元素...

知道这是否是一个已知的错误吗?

无法在 IE11 和 Chrome 上重现。

为了更好地理解这个问题,这里有一个例子:

Codepen

/* this is just so that the squares are similarly displayed */
section {
  position: relative;
  display: inline-block;
  margin-right: 75px;  
}

div {
  background-color: red;
  width: 100px;
  height: 100px;
  color: white;
  padding: 5px;
}

div:focus {
  background-color: green;
}

div > span {
  position: absolute;
  display: inline-block;
  top: 50px;
  left: 50px;
  background-color: blue;
  width: 100px;
  height: 100px;
  padding: 5px;
}
Context: an absolute element inside a focusable element.<br>
In  Firefox 36, if the focusable element does not have a "position: relative", a click on the inside element will not set the focus on the focusable element...<br>
(red block turns green when focused)
<br><br>
Edit: none works in IE
<br><br>

<section>
  <div style="position: relative;" tabindex="-1">
    With position: relative
    <span>
      click here
    </span>
  </div>
</section>

<section>
  <div tabindex="-1">
    With no position
    <span>
      click here
    </span>
  </div>
</section>

【问题讨论】:

  • 我想我不妨在 bugzilla 上发布一个错误报告。这是:bugzilla.mozilla.org/show_bug.cgi?id=1138028
  • 你能解释一下为什么你不想或不能使用“position: relative;”吗?
  • 老实说,我不记得了。我想我最终做了不同的事情。但这不是重点。关键是这种行为没有意义。
  • 我实际上认为它确实有道理。牢记这条评论:bugzilla.mozilla.org/show_bug.cgi?id=1138028#c1
  • 这在 FF 66 中看起来已修复

标签: css firefox position focus css-position


【解决方案1】:

我遇到了同样的问题,并改用 mousedown 事件解决了。

【讨论】:

    【解决方案2】:

    在这种情况下,点击事件被触发并且事件传播将发生。因此,单击内部元素将传播到父元素,父元素将获得焦点并应用样式。更多详情请通过事件传播event propagation

    【讨论】:

      【解决方案3】:

      在以下情况下可以在所有浏览器中重现:

      <!doctype html>
      <html lang="en">
      <head>
          <title>Focus</title>
          <style>
          button { display: inline-block; width: 30px; height: 30px; background-color: red; }
          button:focus { background-color:green; }
          span { position: absolute; display:inline-block; width:100px }
          </style>
      </head>
      <body>
            <button>
              <span tabindex="1">
                click here
          </span>
            </button>
      </body>
      </html>
      

      并且tabindex 不能保证添加focus,原因如下:

      仅因其 tabindex 属性而可聚焦的元素将触发点击事件以响应非鼠标激活

      可访问性指南描述了这样的解决方法:

      提供对脚本的访问的技术包括:

      允许用户配置用户代理,以便 mouseover/mouseout 事件处理程序由(和激活)焦点/模糊事件激活。同样,允许用户使用键盘命令,例如enter 和Shift-Enter 来触发onclick 和ondblclick 事件。

      使用单个激活事件实现“文档对象模型 (DOM) 级别 2 事件规范”[DOM2EVENTS] 事件,并提供使用每个受支持的输入设备或输入 API 触发该事件的方法。这些应该与上面提供的单击事件和映射相同(但请注意,同时也是编辑器的用户代理可能希望使用单击事件来移动系统插入符号,并希望提供不同的行为来使用鼠标激活)。

      例如,Amaya [AMAYA] 使用doAction 命令来激活链接和表单元素,可以通过鼠标(并且可以将其设置为单击或双击)或通过键盘(可以使用 Amaya 的键盘配置为任意键设置)

      对于作者:记录已知重要脚本的效果,以便让用户提前了解他们所做的事情。通过使用(标记语言)规范的相关元素或属性来做到这一点,或者如果没有,请提供脚本行为的描述。

      此外,此行为未指定:

      :focus 伪类在元素具有焦点时应用(接受键盘事件或其他形式的文本输入)。

      一个元素可以同时匹配多个伪类。

      CSS 没有定义哪些元素可能处于上述状态,或者状态如何进入和离开。

      使用 focusin/focusout polyfill 来规范跨浏览器行为:

      function focusPolyfill()
        {
        'use strict';
        
        var w = window,
        d = w.document;
      
        function addPolyfill(e)
          {
          var 
      		type = e.type === 'focus' ? 'focusin' : 'focusout',
      		event = new CustomEvent(type, { bubbles:true, cancelable:false });
          event.c1Generated = true;
          e.target.dispatchEvent( event );
          }
      
        function removePolyfill(e)
          {
          if(!e.c1Generated)
            { 
            // focus after focusin, so chrome will the first time trigger tow times focusin
            d.removeEventListener('focus' ,addPolyfill ,true);
            d.removeEventListener('blur' ,addPolyfill ,true);
            d.removeEventListener('focusin' ,removePolyfill ,true);
            d.removeEventListener('focusout' ,removePolyfill ,true);
            }
      	}
      	
         function removeHandler()
          {
          d.removeEventListener('focusin' ,removePolyfill ,true);
          d.removeEventListener('focusout' ,removePolyfill ,true);
          }
          
        
        if( w.onfocusin === undefined )
          {
          d.addEventListener('focus' ,addPolyfill ,true);
          d.addEventListener('blur' ,addPolyfill ,true);
          d.addEventListener('focusin' ,removePolyfill ,true);
          d.addEventListener('focusout' ,removePolyfill ,true);
          }
        
        w.setTimeout(removeHandler);
        }
         

      以下是焦点在元素之间转移时的典型事件序列,包括已弃用的 DOMFocusIn 和 DOMFocusOut 事件。显示的顺序假定最初没有元素被聚焦。

      用户转移焦点 1. focusin 在第一个目标元素获得焦点之前发送 2. focus 在第一个目标元素获得焦点后发送 3. DOMFocusIn 如果支持 用户转移焦点 4. focusout 在第一个目标元素失去焦点之前发送 5. focusin 在第二个目标元素获得焦点之前发送 6. blur 在第一个目标元素失去焦点后发送 7. DOMFocusOut 如果支持 8. focus 在第二个目标元素获得焦点后发送 9. DOMFocusIn 如果支持

      参考文献

      【讨论】:

      • 您的可聚焦元素有一个位置。问题是当它没有职位时(而且我不想要职位)。我尝试简单地添加标签,但它仍然不起作用。
      【解决方案4】:

      尝试使用&lt;p&gt; 标签及其工作。

      div > p{
        display: inline-block;
        top: 50px;
        left: 50px;
        background-color: blue;
        width: 100px;
        height: 100px;
        padding: 5px;
        margin-left:40px;
      }
      
      <section>
        <div tabindex="-1">
          With no position
          <p>
            click here
          </p>
        </div>
      </section>
      

      链接http://codepen.io/anon/pen/MYLeLp

      【讨论】:

      • 当然是,你的p不是绝对的。关键是元素必须有一个绝对位置。
      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多