jQuery 拖放(Drag and Drop)是一种用户界面交互技术,允许用户通过鼠标操作将元素从一个位置移动到另一个位置。jQuery UI 是一个流行的库,提供了拖放功能的实现。
以下是一个简单的 jQuery UI 拖放示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery UI Drag and Drop</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<style>
#draggable {
width: 100px;
height: 100px;
background-color: #ccc;
text-align: center;
line-height: 100px;
}
#droppable {
width: 200px;
height: 200px;
background-color: #eee;
text-align: center;
line-height: 200px;
}
</style>
</head>
<body>
<div id="draggable">Drag me</div>
<div id="droppable">Drop here</div>
<script>
$(function() {
$("#draggable").draggable();
$("#droppable").droppable({
drop: function(event, ui) {
$(this).addClass("ui-state-highlight").find("p").html("Dropped!");
}
});
});
</script>
</body>
</html>
containment
选项限制拖放区域。通过以上内容,你应该对 jQuery 拖放有了全面的了解,并能够解决常见的拖放问题。