欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery插件zTree实现的基本树与节点获取操作示例

程序员文章站 2023-11-26 14:07:10
本文实例讲述了jquery插件ztree实现的基本树与节点获取操作。分享给大家供大家参考,具体如下: 1、实现代码: ...

本文实例讲述了jquery插件ztree实现的基本树与节点获取操作。分享给大家供大家参考,具体如下:

1、实现代码:

<!doctype html>
<html>
<head>
  <title>ztree实现基本树</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <link rel="stylesheet" type="text/css" href="ztree_v3/css/demo.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="ztree_v3/css/ztreestyle/ztreestyle.css" rel="external nofollow" >
  <script type="text/javascript" src="ztree_v3/js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="ztree_v3/js/jquery.ztree.core.min.js"></script>
  <script type="text/javascript">
    <!--
    var setting = {
      data: {
        simpledata: {
          enable: true
        }
      }
    };
    var znodes =[
      { id:1, pid:0, name:"湖北省", open:true},
      { id:11, pid:1, name:"武汉市", open:true},
      { id:111, pid:11, name:"汉口"},
      { id:112, pid:11, name:"汉阳"},
      { id:113, pid:11, name:"武昌"},
      { id:12, pid:1, name:"黄石市"},
      { id:121, pid:12, name:"黄石港区"},
      { id:122, pid:12, name:"西塞山区"},
      { id:123, pid:12, name:"下陆区"},
      { id:124, pid:12, name:"铁山区"},
      { id:13, pid:1, name:"黄冈市"},
      { id:2, pid:0, name:"湖南省", open:true},
      { id:21, pid:2, name:"长沙市", open:true},
      { id:211, pid:21, name:"芙蓉区"},
      { id:212, pid:21, name:"天心区"},
      { id:213, pid:21, name:"岳麓区"},
      { id:214, pid:21, name:"开福区"},
      { id:22, pid:2, name:"株洲市"},
      { id:221, pid:22, name:"天元区"},
      { id:222, pid:22, name:"荷塘区"},
      { id:223, pid:22, name:"芦淞区"},
      { id:224, pid:22, name:"石峰区"},
      { id:23, pid:2, name:"湘潭市"},
      { id:231, pid:23, name:"雨湖区"},
      { id:232, pid:23, name:"岳塘区"},
      { id:233, pid:23, name:"湘乡市"},
      { id:234, pid:23, name:"韶山市"}
    ];
    $(document).ready(function(){
      $.fn.ztree.init($("#basetree"), setting, znodes);
    });
    /**
     * 获取选中节点的编码和名称
     */
    function findselect()
    {
       var treeobj = $.fn.ztree.getztreeobj("basetree");
       var nodes = treeobj.getselectednodes();
       for(var i=0;i<nodes.length;i++)
       {
         var nodeid = nodes[i].id;
         var nodename = nodes[i].name;
         alert("树节点编码:"+nodeid+"\n"+"树节点名称:"+nodename);
       }
    }
    //-->
  </script>
 </head>
<body>
<div class="content_wrap">
  <div class="ztreedemobackground left" style="text-align: center;">
    <ul id="basetree" class="ztree" style="height: 300px; width:200px; overflow-y: auto"></ul>
    <input type="button" id="btn" onclick="findselect()" value="获取选中节点"/>
  </div>
</div>
</body>
</html>

2、设计效果图:

(1)初始化

jQuery插件zTree实现的基本树与节点获取操作示例

(2)单击按钮

jQuery插件zTree实现的基本树与节点获取操作示例

3、设计说明

获取 ztree 当前被选中的节点数据集合

var treeobj = $.fn.ztree.getztreeobj("basetree");
var nodes = treeobj.getselectednodes();

附:ztree_v3插件点击此处本站下载

更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery常用插件及用法总结》、《jquery中ajax用法总结》、《jquery表格(table)操作技巧汇总》、《jquery扩展技巧总结》、《jquery常见经典特效汇总》及《jquery选择器用法总结

希望本文所述对大家jquery程序设计有所帮助。