簡體   English   中英

使元素在其自身的mousedown事件上隱式拖動

[英]Make element draggable on mousedown event of its own implicitely

您好,我正在嘗試使用以下代碼來使clicked元素在運行時可拖動,但是在第一次單擊其工作正常后,其第一次單擊對任何元素都不起作用,我不知道為什么會發生,請幫幫我。 礦山代碼如下:

$(document).ready(function(){   
    $(document).mousedown(function(event) {
        $("#"+$(event.target).attr("id")).draggable();
        console.log($(event.target).attr("id"));
    });
});

您的.draggable調用位於mousedown處理程序中-這樣,在首次單擊時mousedown其初始化。 您可以在$(document).ready處理程序中對其進行初始化,以使其在第一次單擊時起作用:

$(document).ready(function(){   
    $("div.dragMe").draggable();
    $(document).mousedown(function() {
        console.log($(this).attr("id"));
    });
});

在此代碼中,所有具有class="dragMe" div都將變為可拖動的。

暫無
暫無

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

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