当文档位于禁用元素上时,不会触发该文档的事件

问题描述 投票:1回答:1

当我给文档一个mouseup事件时,如果鼠标位于禁用的元素上并且我释放了按钮。无法触发文档mouseup事件处理程序。我应该如何让处理程序被调用?

示例:

document.addEventListener('mouseup', () => {
  console.log('mouseup')
}, true)

const changeBtn = document.getElementById('change')
const testBtn = document.getElementById('test')

changeBtn.onclick = () => {
  testBtn.setAttribute('disabled', true)
}
testBtn.onclick = () => {
  console.log('testBtn click')
}
<button id="test">test</button>
<button id="change">change</button>
javascript html dom frontend dom-events
1个回答
2
投票

pointer-events: none元素上使用CSS集pointer-events: none

:disabled
:disabled
document.addEventListener('mouseup', () => {
  console.log('mouseup')
}, true)

const changeBtn = document.getElementById('change')
const testBtn = document.getElementById('test')

changeBtn.onclick = () => {
  testBtn.setAttribute('disabled', true)
}
testBtn.onclick = () => {
  console.log('testBtn click')
}
© www.soinside.com 2019 - 2024. All rights reserved.