为什么在同一元素上的mouseDown事件触发后不点击事件触发?

时间:2021-06-21 20:34:39

I have a mouseDown event and a click event on the same element. When I click on it, the mouseDown event fires (i.e. alerts "Mouse pressed on P"), but the click event doesn't. However, if I comment out the mouseDown alert statement, the click event does display its alert. Why is this? http://jsfiddle.net/A8vhq/

我在同一个元素上有一个mouseDown事件和一个click事件。当我点击它时,mouseDown事件触发(即警告“鼠标按下P”),但点击事件不会。但是,如果我注释掉mouseDown警告语句,则click事件会显示其警报。为什么是这样? http://jsfiddle.net/A8vhq/

3 个解决方案

#1


6  

That's because the click never happens, when the alert box appears it interrupts the mouseup event on the element, thus interrupting mouseclick.

这是因为点击永远不会发生,当出现警告框时,它会中断元素上的mouseup事件,从而中断鼠标点击。

use console.log('message') to test your code instead of alert.

使用console.log('message')来测试代码而不是警报。

#2


0  

Instead of alert, use console.log and it will work.

而不是警报,使用console.log,它将工作。

Live demo: http://jsfiddle.net/A8vhq/1/

现场演示:http://jsfiddle.net/A8vhq/1/


Btw, I've refactored the code a bit:

顺便说一句,我稍微重构了一下代码:

$('p').on('mousedown', function (e) {
    console.log('Mouse pressed on ' + e.target.nodeName);
});

$('p').on('click', function (e) {
    console.log(e.target.nodeName + ' clicked');
});

#3


0  

Just replace alert with console.log or any other non-modal output way.

只需用console.log或任何其他非模态输出方式替换alert。

#1


6  

That's because the click never happens, when the alert box appears it interrupts the mouseup event on the element, thus interrupting mouseclick.

这是因为点击永远不会发生,当出现警告框时,它会中断元素上的mouseup事件,从而中断鼠标点击。

use console.log('message') to test your code instead of alert.

使用console.log('message')来测试代码而不是警报。

#2


0  

Instead of alert, use console.log and it will work.

而不是警报,使用console.log,它将工作。

Live demo: http://jsfiddle.net/A8vhq/1/

现场演示:http://jsfiddle.net/A8vhq/1/


Btw, I've refactored the code a bit:

顺便说一句,我稍微重构了一下代码:

$('p').on('mousedown', function (e) {
    console.log('Mouse pressed on ' + e.target.nodeName);
});

$('p').on('click', function (e) {
    console.log(e.target.nodeName + ' clicked');
});

#3


0  

Just replace alert with console.log or any other non-modal output way.

只需用console.log或任何其他非模态输出方式替换alert。