javascript - 是否可以在不中断单击事件的情况下对子级 onmousedown 重新排序

标签 javascript

我想在鼠标按下时更改子元素的索引,但不中断单击事件。这可能吗?

<script>

function mouseDownHandler(event) {
    var p = event.currentTarget.parentNode;
    p.appendChild(event.currentTarget);

}
</script>

<div style="position: absolute; left: 50px; top: 100px; width: 50px; height: 100px; background-color: red;" onmousedown="mouseDownHandler(event)" onclick="console.log('click 1')">This is a test</div>


<div style="position: absolute; left: 70px; top: 100px; width: 50px; height: 100px; background-color: blue;" onmousedown="mouseDownHandler(event)" onclick="console.log('click')">This is a test</div>

https://jsfiddle.net/9d2dvcqj/

最佳答案

当在同一个元素上发生 mousedownmouseup 事件时,将处理

Click 事件。因此,当元素的顺序发生变化时,这种情况不会发生,并且不会调用 click 事件。

一种解决方案 - 记住哪个元素发生了 mousedown 事件,并将其与 mouseup 事件的发生进行比较:

function mouseDownHandler(event) {
    var p = event.currentTarget.parentNode;
  mouseDownHandler.mde = event.currentTarget;
    p.appendChild(event.currentTarget);
}

function mouseUpHandler(event) {
  if (event.currentTarget === mouseDownHandler.mde) {
    document.body.innerHTML += 'click<br/>';
  } else {
    mouseDownHandler.mde = null;
  }
}

https://jsfiddle.net/nrd98ujr/

关于javascript - 是否可以在不中断单击事件的情况下对子级 onmousedown 重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35611989/

相关文章:

c# - 如何使用 Selenium Driver 检测页面是否水平溢出?

javascript - 为数组中的每两个元素创建对

javascript - Reactjs - 为什么我的 MaterialUI 数据表不刷新?

javascript - 移动浏览器上的 AngularJS 和 ASP.Net WebAPI 社交登录

javascript - 滚动时淡出 div

javascript - 抓取拖放的项目作为 <div> 的子节点

等效于 LINQ Any() 的 JavaScript/jQuery

javascript - 获取字符串的所有组合

Javascript - 基于动态填充下拉菜单的转到 url

javascript - 处理 RequireJS 必需类