0
您是否需要为每个可拖动元素指定不同的名称?我有3张图片,我希望能够拖动它们。我试图给他们所有相同的ID,但它失败了。我将有可变数量的图像,所以我需要它们都具有相同的ID。这里出了什么问题?JQuery可拖动多个项目具有相同的ID不起作用
这里是我的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#draggable { width: 150px; height: 150px; padding: 0.5em; border:1px solid black;}
</style>
<script>
$(document).ready(function(){
$("#draggable").draggable();
});
</script>
</head>
<body>
<img id="draggable" src="small/Koala.jpg"/>
<img id="draggable" src="small/Desert.jpg"/>
<img id="draggable" src="small/Tulips.jpg"/>
</body>
</html>
现在不会克隆 - 我要克隆的图像和克隆拖放到可投放箱。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Wall Builder</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
.draggable { width: 85px; height: 85px; padding: 0.5em; border:1px solid black;}
.droppable { width: 150px; height: 150px; padding: 0.5em; border:1px solid black;}
</style>
<script>
$(document).ready(function() {
$(".draggable").draggable({
revert: "invalid"
,helper: 'clone'
});
$(".droppable").droppable({
accept: "draggable"
});
});
</script>
</head>
<body>
<img class="draggable" id="1" src="small/Koala.jpg"/>
<img class="draggable" id="2" src="small/Desert.jpg"/>
<img class="draggable" id="3" src="small/Tulips.jpg"/>
<div class="droppable" id="d1"></div>
<div class="droppable" id="d2"></div>
<div class="droppable" id="d3"></div>
<div class="droppable" id="d4"></div>
<div class="droppable" id="d5"></div>
</body>
</html>
好的,谢谢!如果我想克隆,是否还需要所有可拖拽元素的ID? – user1261710
这个问题有点模糊,所以我不知道如何回答...一般来说,无论你选择,并说'.clone()'上会被克隆,所以如果你只是想克隆一个东西只能选择一件事(或者通过给与某个id相关的东西并选择它,或者通过该类选择并进行数组索引,或者:首先或类似的东西)。听起来你可能还需要阅读jQuery选择器(http://www.w3schools.com/jquery/jquery_ref_selectors.asp) – Colleen
它不会克隆。我不知道为什么。 – user1261710