英文:
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:
<span disabled>Disabled</span>
<span>Open to events</span>
In js:
document.querySelector('span:not([disabled=""])').addEventListener("event", 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(
<div>
<span
onClick={(e) => log(e)}
onKeyDown={(e) => log(e)}
onFocus={(e) => log(e)}
>
tabIndex none
</span>
<span tabIndex={0}
onClick={(e) => log(e)}
onKeyDown={(e) => log(e)}
onFocus={(e) => log(e)}
>
tabIndex 0
</span>
<span
style={{pointerEvents: "none"}}
onClick={(e) => log(e)}
onKeyDown={(e) => log(e)}
onFocus={(e) => log(e)}
>
pointer-events none
</span>
</div>
, document.getElementById("root"));
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 -->
<p>
click on a span and then type some text using keyboard
</p>
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论