Prevent event.click from triggering event.mousemove in Safari
It seems that in jQuery, a cl开发者_StackOverflow中文版ick event on an element will automatically trigger a mousemove event for that element as well. Is there any way to prevent this?
Here is the most basic test case I could put together. For me, using a trackpad, a click on the box triggers the mousemove event. And I did not notice any movement in the mouse cursor.
http://jsfiddle.net/nay5d/
UPDATE: I have tested the code in both Safari, Chrome, and Firefox. In Safari, a click triggers mousemove. In Chrome/Firefox, it seems that a click DOES NOT trigger mousemove. Interesting.
I get the same result using a legacy method in Safari, as well. The extra mouseMove
event is being triggered on mouseUp
. Example: http://jsfiddle.net/s7r8d/
document.onmousemove = function(mouseEvent) {
mouseEvent || (mouseEvent = window.event);
var target = mouseEvent.target || mouseEvent.srcElement;
if (target.id=="dom") {
target.className = target.className=="highlight"
? ""
: "highlight";
}
}
Even trying to coerce the mouseUp
event is unsuccessful. We can try reporting this bug to Apple to fix if it indeed is a Safari bug.
I just ran a few tests, and I don't feel like this is what is happening. If you don't register a mousemove event with an element, then no event will fire. If you ARE registering a mousemove event, then you should be aware than the littlest bit of mouse movement (including the movement that happens when you click the object) will cause the mousemove event to be fired.
Depending on what you're trying to accomplish, you may want to look into one of the many other mouse events, or accomplish your functionality in a completely different manner. (If you post what your ultimate goal is for this bit of functionality, I can try to help you with that.)
Hy guys, I tried like this and seems to work, at least in chrome
http://jsfiddle.net/ZNHDT/
$('h1').bind('mousemove', swapClass);
function swapClass() {
$('h1').toggleClass('highlight');
}
$('h1').mousedown(function(ev){
$("h1").unbind("mousemove");
console.log("pippo");
})
$('h1').mouseup(function(){
setTimeout(function(){
$("h1").bind("mousemove", swapClass);
},10);
});
I think not, as long as your mouse is on the click area, you'll be firing the mousemove event too.
Here's an example: http://jsfiddle.net/marcosfromero/3KHQG/
精彩评论