[英]Make element draggable on mousedown event of its own implicitely
Hello I am trying with following code to make clicked element draggable at runtime, but its not working for any element for its very first click after first click its working fine, I dont know why it happen,Please help me out. 您好,我正在尝试使用以下代码来使clicked元素在运行时可拖动,但是在第一次单击其工作正常后,其第一次单击对任何元素都不起作用,我不知道为什么会发生,请帮帮我。 Mine code is as follows:- 矿山代码如下:
$(document).ready(function(){
$(document).mousedown(function(event) {
$("#"+$(event.target).attr("id")).draggable();
console.log($(event.target).attr("id"));
});
});
Your .draggable
call is in the mousedown
handler - that way it is initialized on the first click. 您的.draggable
调用位于mousedown
处理程序中-这样,在首次单击时mousedown
其初始化。 You might initialize it in the $(document).ready
handler instead to make it work on first click: 您可以在$(document).ready
处理程序中对其进行初始化,以使其在第一次单击时起作用:
$(document).ready(function(){
$("div.dragMe").draggable();
$(document).mousedown(function() {
console.log($(this).attr("id"));
});
});
In this code, all div
s having the class="dragMe"
would be made draggable. 在此代码中,所有具有class="dragMe"
div
都将变为可拖动的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.