如何禁用任何HTML元素,使其不会被任何事件处理,类似于禁用输入元素?

huangapple go评论74阅读模式
英文:

How to disable any HTML element so it won't be handled by any event, similar to input element being disabled?

问题

When an input element is disabled, it won't be handled by any event.

如何实现对于任何元素(例如 span 元素)?

我尝试使用 pointer-events: "none",但它只阻止了点击事件。其他事件如 onKeyDown 仍然可以被调用。

请看示例:

专注于 tab 键选定 span,然后点击空格键。将会调用提供给 onKeyDown 属性的函数。我希望该函数不被调用。

英文:

When an input element is disabled, it won't be handled by any event.

<input disabled />

How to achieve that for any element (e.g. a span element)?

I've tried using pointer-events: "none" but it only prevents click events. Other event such as onKeyDown can still be called.

Here is an example :

import * as React from "react";

export default function UnstyledSwitches() {
  return (
    <div>
      <span tabIndex={0} onKeyDown={() => console.log("clicked")}>
        test
      </span>
    </div>
  );
}

https://codesandbox.io/s/base-cra-typescript-forked-43708i?file=/src/App.tsx

Focus on the span with tab and then click spacebar. The function that is supplied to onKeyDown props will be called. I want the function to not be called.

答案1

得分: 2

一种解决方法是向元素添加disabled属性,然后使用JavaScript来限制eventListeners不作用于已禁用的元素。

例如:

在HTML中:

<span disabled>已禁用</span>
<span>接受事件</span>

在JavaScript中:

document.querySelector('span:not([disabled])').addEventListener("event", handler);

但是,如果元素在动态地禁用/启用,你需要在每次元素状态更改后添加事件监听器。

英文:

One solution can be to add the disabled attribute to the element and then use javascript to limit eventListeners to not-disabled elements.

For example:

In html:

 &lt;span disabled&gt;Disabled&lt;/span&gt;
 &lt;span&gt;Open to events&lt;/span&gt;

In js:

document.querySelector(&#39;span:not([disabled=&quot;&quot;])&#39;).addEventListener(&quot;event&quot;, handler);

But, if elements are being disabled/enabled dynamically, you'd need to add eventListeners after each change in element's state.

答案2

得分: 1

Sure, here are the translated parts:

pointerEvents and tabIndex

这个问题问如何防止 span 元素上的键盘输入。然而,问题中的代码给元素的 tabIndex 赋了一个值。设置 tabIndex(或 contentEditable)属性允许元素获得焦点并接收键盘事件。

从 span 元素中移除 tabIndex 会阻止获得焦点和键盘事件,但不会影响鼠标事件。要同时阻止键盘和鼠标事件,将 pointerEvents 设置为 "none"。tabIndex 设置似乎对 span 元素没有影响,但对输入框可能会有影响。

问题的提问者指出 pointerEvents "none" 没有起作用,但在示例中却起作用了。原因不清楚,可能是因为测试方法不同。

React 代码片段

这个代码片段包含了一些具有不同配置的 span 元素以进行尝试。

英文:

pointerEvents and tabIndex

The question asks how to prevent keyboard input on a span. However, the code included in the question assigns a value to the element's tabIndex. Setting the tabIndex (or contentEditable) attribute allows the element to receive focus along with keyboard events.

Removing the tabIndex from the span prevents focus and keyboard events, but not mouse events. To prevent both keyboard and mouse events, set pointerEvents to "none". The tabIndex setting doesn't appear to make any difference with a span, but might with an input.

OP notes that pointerEvents "none" did not work and yet in the snippet it does. The reason is unclear, but perhaps it due to different testing methods.

React Snippet

The snippet contains a few span elements with different configurations to try.

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

const {useState} = React;

ReactDOM.render(
    
      &lt;div&gt;
      
      &lt;span  
        onClick={(e) =&gt; log(e)}
        onKeyDown={(e) =&gt; log(e)}
        onFocus={(e) =&gt; log(e)}
      &gt;
        tabIndex none
      &lt;/span&gt;
      
      &lt;span tabIndex={0} 
        onClick={(e) =&gt; log(e)}
        onKeyDown={(e) =&gt; log(e)}
        onFocus={(e) =&gt; log(e)}
      &gt;
        tabIndex 0
      &lt;/span&gt;
      
      &lt;span 
        style={{pointerEvents: &quot;none&quot;}}
        onClick={(e) =&gt; log(e)}
        onKeyDown={(e) =&gt; log(e)}
        onFocus={(e) =&gt; log(e)}
      &gt;
        pointer-events none
      &lt;/span&gt;

    &lt;/div&gt;
  
, document.getElementById(&quot;root&quot;));


function log(e) { console.log(e.type) }

<!-- language: lang-css -->

.pointer-events-none {
  pointer-events: none;
}

span {
  margin: 0.5rem;
  padding: 1rem;
  border: thin dotted blue;
  border-radius: 0.5rem;
}

span:focus {
  background-color: lightyellow;
}

body {
  font-family: sans-serif;
  margin: 1rem;
}
.as-console-wrapper { max-height: 100px !important; }

<!-- language: lang-html -->

&lt;p&gt;
click on a span and then type some text using keyboard
&lt;/p&gt;

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 04:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定