jQuery UI Sortable toArray()方法
jQuery UI是一种基于网络的技术,由各种GUI部件、视觉效果和主题组成。这些功能可以使用jQuery JavaScript库来实现。jQuery UI是为网页建立UI界面的最佳工具。它也可以用来建立高度互动的网络应用程序,或者可以用来轻松地添加小工具。
在这篇文章中,我们将使用jQuery UI Sortable toArray()方法,将可排序的项目ID序列化为一个字符串数组。
语法:
$(".selector").sortable("toArray" );
参数:该方法接受单参数选项,用于定制序列化。它包含具有默认 “id “的字符串类型的属性,该属性持有用于数值的属性名称。
返回值:该方法按排序顺序返回可排序元素的id值数组。
CDN链接:首先,添加你的项目需要的jQuery Mobile脚本。
<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>
<link rel=”stylesheet” href=”https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
例子:下面的例子说明了jQuery UI Sortable toArray()方法的使用。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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>
<title>jQuery UI Sortable toArray() Method</title>
<style>
#sortableList {
list-style-type: none;
}
.geeks {
margin: 10px;
background: lightgreen;
padding: 10px;
border: 1px solid green;
font-size: 25px;
}
</style>
<script>
(function () {
('#sortableList').sortable({
update: function (event, ui) {
var benefits = (this)
.sortable('toArray').toString();
("#sortedList").text(benefits);
}
});
});
</script>
</head>
<body>
<center>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h4>jQuery UI Sortable toArray() Method</h4>
<ul id="sortableList">
<li id="Tutorials" class="geeks">
1.Free Tutorials
</li>
<li id="Articles" class="geeks">
2.Millions of articles
</li>
<li id="Webinars" class="geeks">
3.Webinars by Industry Experts
</li>
<li id="Courses" class="geeks">
4.Live, Online and Classroom Courses
</li>
</ul>
<h3>Benefits ID after sorting</h3>
<h2><span id='sortedList'></span></h2>
</center>
</body>
输出: