jQuery UI sortable connectWith选项
jQuery UI是一种基于网络的技术,由GUI部件、视觉效果和使用jQuery、JavaScript库实现的主题组成。它也可以用来建立高度互动的网络应用程序,或者可以用来轻松添加小工具。
在这篇文章中,我们将学习如何使用jQuery UI Sortable connectWith选项。使用这个选项,一个其他可排序元素的选择器,这个列表中的项目应该被连接到。要想双向连接,connectWith选项必须在两个可排序元素上设置。这个选项的默认值是 “false”。
语法:
connectWith选项需要一个选择器类型的值,语法如下。
$( ".selector" ).sortable({
connectWith: "#shopping-cart"
});
- 获取connectWith选项
var connectWith = $( ".selector" )
.sortable( "option", "connectWith" );
- 设置connectWith选项
$( ".selector" ).sortable( "option",
"connectWith", "#shopping-cart" );
CDN链接:你的项目将需要以下jQuery Mobile脚本,所以我们需要将这些脚本添加到你的项目中。
<link rel=”stylesheet” href=”https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<script src= “https://code.jquery.com/jquery-1.12.4.js”></script>
<script src= “https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>
例子:这个例子描述了jQuery UI Sortable connectWith选项的用途。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet"
href=
"https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src=
"https://code.jquery.com/jquery-1.12.4.js">
</script>
<script src=
"https://code.jquery.com/ui/1.12.1/jquery-ui.js">
</script>
<style>
.list {
background-color: green;
border: 1px solid gray;
}
.items {
list-style-type: none;
margin-left: 5px;
padding: 0;
width: 200px;
float: left;
}
.items li {
margin: 5px;
padding: 5px;
cursor: pointer;
border-radius: 5px;
font-size: 20px;
}
.o {
background-color: #00ff44;
}
</style>
<script>
(function () {
("#sortable1,#sortable3").sortable({
connectWith: "#sortable3",
dropOnEmpty: true
});
("#sortable2").sortable({
connectWith: "#sortable3",
dropOnEmpty: false
});
("#sortable1, #sortable2, #sortable3").disableSelection();
});
</script>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h4>jQuery UI Sortable connectWith Option</h4>
<ul id="sortable1" class="items">
<li class="list">Drop on empty</li>
<li class="list">Drop on empty</li>
</ul>
<ul id="sortable2" class="items">
<li class="list o">No drop on empty</li>
<li class="list o">No drop on empty</li>
</ul>
<ul id="sortable3" class="items"
style="height:200px;background-color:#a0ff94">
</ul>
</center>
</body>
</html>
输出:
jQuery UI sortable connectWith选项