问题描述:

I want to be able to trigger the event and have it show that there was actually a button that was down at the time the event was fired.

$('table').on('mousemove', function(e) {

var is_dragging = e.buttons.length > 0;

// is_dragging should === true;

});

$('table').trigger('mousemove'); // is_dragging will be false

网友答案:

Try this out. Creating a custom event and attaching buttons property to it.

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons.length > 0;
  // is_dragging should === true;
});

var e = $.Event( 'mousemove' );
e.buttons = ['<set any key you need>'];

$('table').trigger(e); 
网友答案:

::UPDATE::

look into ondragstart http://www.w3schools.com/jsref/event_ondragstart.asp

=======

*.buttons is for what button you have clicked. At the time of mousemove you have not clicked a button.

Meaning your e.buttons === 0

Documentation on MouseEvent.button: http://www.w3schools.com/jsref/event_button.asp

You can verify this by throwing a debugger in your function and inspecting your e variable

JSFiddle: http://jsfiddle.net/qf3u8m61/

网友答案:

So based on what i think you are trying to accomplish. You want to have an event handler for onmousemove where you are checking to see if a button is down (doesn't matter which button is down) however you want to manually trigger the event and have the condition is_dragging result in true.

If the event is manually triggered using $('table').trigger('mousemove');the event will not have a buttons property however it will have a .isTrigger property which will == 3.

Try this: https://jsfiddle.net/SeanWessell/L2z0su3j/

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons > 0 || e.buttons == undefined && e.isTrigger == 3;
  // is_dragging should === true;
  $('p').html(is_dragging.toString())
});

$('#trigger').click(function(){

    $('table').trigger('mousemove');

})
相关阅读:
Top