• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

jQuery实现li元素顺序变更代码示例

jquery 程序员 7年前 (2019-01-16) 240次浏览 已收录 0个评论

本文实现了li元素顺序变更代码示例

代码实例如下:

<!DOCTYPE html>
<html> 
<head> 
<meta charset=" utf-8"> 
<title>jQuery实现li元素顺序变更代码示例-搞代码gaodaima.com</title>
<style type="text/css">
ul{
  list-style-type:none;
  float:left;
  margin-right:30px;
  background-color:Green;
  width:100px;
  height:100px;
  padding:0px;
}
li{
  margin-bottom:5px;
  background-color:Red;
  cursor:pointer;
  font-size:12px;
  height:25px;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> 
<script type="text/javascript">
var json = [
  { "id": "1", "webName": "搞代码", "Age": "3"},
  { "id": "2", "webName": "hczx123.com", "Age": "1"},
  { "id": "3", "webName": "腾讯", "Age": "15"},
  { "id": "4", "webName": "网易", "Age": "10"}
];
$(function(){
  var $topUl = $("#topul");
  var $bottomUl = $("#bottomul");
  for (var index = 0; index < json.length; index++) {
    $Li = $("<li>" + json[index].webName + "," + json[index].Age + "岁</li>");
    $Li.click(function () {
      if ($(this).parent().attr("id") == "topul") {
        $(this).appendTo($bottomUl); 
      }
      else {
        $(this).appendTo($topUl);
      }
    });
    $topUl.append($Li);
  }
});
</script>
</head>
<body>
<ul id="topul"></ul>
<ul id="bottomul"></ul>
</body>
</html>

上面的代码实现了我们的要求,下面介绍一下它的实现过程。

一.代码注释:

(1).var json = [],这是一个数组,里面存储我们需要的数据。

(2).$(function(){}),当文档结构完全加载完毕再去执行函数中的代码。

(3).var $topUl = $(“#topul”),获取id属性值为topul的元素。

(4).var $bottomUl = $(“#bottomul”),获取id属性值为bottomul的元素。

(5).for (var index = 0; index < json.length; index++) {},对数组进行遍历操作。

(6).$Li = $(“<li>” + json[index].webName + “,” + json[index].Age + “岁</li>”),创建li元素。

(7).$Li.click(function () {}),为li元素注册click事件处理函数。

(8).if ($(this).parent().attr(“id”) == “topul”) {

$(this).appendTo($bottomUl);

},如果当前li元素的父元素的id属性值,那么就将当前li元素移动到id属性值为bottomUl的ul中。

(9).$topUl.append($Li),将创建的li元素添加到ul中。

本文案例为大家展示了《jQuery实现li元素顺序变更代码示例》的代码,若要看代码效果请大家点击“运行代码”按钮。

[code]<!DOCTYPE html> <html> <head> <meta charset=” utf-8″> <title>jQuery实现li元素顺序变更代码示例-搞代码gaodaima.com</title> <style type=”text/css”> ul{   list-style-type:none;   float:left;   margin-right:30px;   background-color:Green;   width:100px;   height:100px;   padding:0px; } li{   margin-bottom:5px;   background-color:Red;   cursor:pointer;   font-size:12px;   height:25px; } </style> <script src=”http://libs.baidu.com/jquery/1.9.0/jquery.js”></script&gt; <script type=”text/javascript”> var json = [   { “id”: “1”, “webName”: “搞代码”, “Age”: “3”},   { “id”: “2”, “webName”: “hczx123.com”, “Age”: “1”},   { “id”: “3”, “webName”: “腾讯”, “Age”: “15”},   { “id”: “4”, “webName”: “网易”, “Age”: “10”} ]; $(function(){   var $topUl = $(“#topul”);   var $bottomUl = $(“#bottomul”);   for (var index = 0; index < json.length; index++) {     $Li = $(“<li>” + json[index].webName + “,” + json[index].Age + “岁</li>”);     $Li.click(function () {       if ($(this).parent().attr(“id”) == “topul”) {         $(this).appendTo($bottomUl);       }       else {         $(this).appendTo($topUl);       }     });     $topUl.append($Li);   } }); </script> </head> <body> <ul id=”topul”></ul> <ul id=”bottomul”></ul> </body> </html>[/code]


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:jQuery实现li元素顺序变更代码示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址