簡體   English   中英

您如何模擬拖動事件(單擊一個按鈕即可刪除)?

[英]How do you simulate a drag event (mousemove with a button clicked)?

我希望能夠觸發事件,並讓它顯示在觸發事件時實際上有一個按下的按鈕。

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

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

試試看 創建一個自定義事件並將buttons屬性附加到它。

$('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); 

因此,基於我認為您正在嘗試實現的目標。 您想為onmousemove提供一個事件處理程序,在該事件處理程序中檢查按鈕是否按下(無論哪個按鈕按下),但是您想要手動觸發事件並使條件is_dragging result為true。

如果使用$('table').trigger('mousemove');手動觸發事件$('table').trigger('mousemove'); 該事件將沒有按鈕屬性,但是將具有.isTrigger屬性,該屬性== 3。

試試這個: 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');

})

::更新::

看看ondragstart http://www.w3schools.com/jsref/event_ondragstart.asp

=======

*.buttons是您單擊的按鈕。 mousemove時,您尚未單擊任何按鈕。

表示您的e.buttons === 0

關於MouseEvent.button的文檔: http : //www.w3schools.com/jsref/event_button.asp

您可以通過在函數中放入debugger並檢查e變量來驗證這一點

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM