[英]jQuery drag an element when mousedown event on its child element
[英]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.