jQuery implements the method of dragging and dropping elements and cookie saves the order

  • 2021-01-02 21:45:11
  • OfStack

This article illustrates how jQuery implements the drag-and-drop method of elements and cookie saves the order. To share for your reference, the details are as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
div
{
border: 1px solid red;
}
#myList
{
padding-top: 20px;
width: 500px;
background: #EEE;
list-style-type: none;
}
#myList li
{
height: 30px;
cursor: pointer;
margin: 20px;
border: 1px solid blue;
}
#myList a
{
text-decoration: none;
color: #0077B0;
}
#myList a:hover
{
text-decoration: underline;
}
#myList .qlink
{
font-size: 12px;
color: #666;
margin-left: 10px;
}
</style>
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#myList").sortable({ delay: 1, containment: "div",connectWith: ".otherlist", stop: function() {
//alert($("#myList").sortable("serialize"));
//alert($("#myList").sortable("toArray"));
$.cookie("myCookie",$("#myList").sortable('toArray'),{ expires: 7 });
//alert($.cookie("myCookie"));
}});
if ($.cookie("myCookie")) {
var ids = $.cookie("myCookie").split(",");
for (var i = 0; i < ids.length; i++) {
$("#" + ids[i]).appendTo("#myList");
}}
});
</script>
</head>
<body>
<div>
<ul id="myList">
<li id="myList_mood"><a href="#"> mood </a> </li>
<li id="myList_photo"><a href="#"> Photo album </a> </li>
<li id="myList_blog"><a href="#"> The log </a> </li>
<li id="myList_vote"><a href="#"> vote </a> </li>
<li id="myList_shate"><a href="#"> share </a> </li>
<li id="myList_group"><a href="#"> group </a> </li>
</ul>
</div>
</body>
</html>

For more information about jQuery, please refer to: Summary of jQuery Drag-and-drop Effects and Techniques, Summary of Common jQuery Classic Effects, Summary of jQuery Extension Techniques, Summary of jquery Selectors and Summary of Common jquery Techniques.

I hope this article has been helpful in jQuery programming.


Related articles: