javascript 将 div 拖入另一个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18031563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drag and insert div into another div
提问by user2640254
I am trying to design a feature that could drag and insert one div into another div.
我正在尝试设计一种可以将一个 div 拖动并插入另一个 div 的功能。
For example:
例如:
<div id="1"> </div>
<div id="2"> </div>
<div id="1"> </div>
<div id="2"> </div>
i want to make #1 draggable (I know it can be done with jQuery, so draggable is not part of my question), and drag #1 over #2, and when mouseup, #2 could be inserted into #1
我想让 #1 可拖动(我知道它可以用 jQuery 完成,所以可拖动不是我的问题的一部分),并将 #1 拖到 #2 上,当鼠标悬停时,#2 可以插入 #1
<div id="1"> <div id="2"> </div> </div>
could somebody explain to me how to achieve that?
有人可以向我解释如何实现这一目标吗?
采纳答案by apaul
You could simplify this quite a bit by using jQuery UI's Sortable
您可以通过使用jQuery UI 的 Sortable来简化这一点
$(document).ready(function () {
addElements();
$(function () {
$("#list1, #list2").sortable({
connectWith: ".lists",
cursor: "move"
}).disableSelection();
});
});
function addElements() {
$("#list1").empty().append(
"<li id='item1' class='list1Items'>Item 1</li>" +
"<li id='item2' class='list1Items'>Item 2</li>" +
"<li id='item3' class='list1Items'>Item 3</li>");
}
回答by Butani Vijay
For demo : Click Here
演示:点击这里
Code :
代码 :
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Droppable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
#droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
</body>
</html>