一个组件监听mousedown和mouseup组件,超出组件范围后,mouseup事件不触发,怎么解决?

比如

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<style>

#root {

display: inline-block;

width: 100px;

height: 100px;

margin: 100px;

background-color: lightblue;

}

</style>

<script type="text/javascript">

window.onload = function () {

const root = document.getElementById("root")

root.addEventListener('mousedown', function (e) {

console.log('mousedown')

})

root.addEventListener('mouseup', function (e) {

console.log('mouseup')

})

console.log('window.onload')

}

</script>

</head>

<body>

<div id="root"></div>

</body>

</html>

图片.png

此时up无法响应

阅读 4.1k
2 个回答

触发在了window了上吧。你window监听一下?

click事件,你按下,然后移走松开,也是不会触发事件的。

你什么场景,需要解决什么问题?

监听一下mouseleave。
或者监听document上面的 mouse 事件,通过 event 的 target 判断具体是那个dom触发的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题