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

jQuery插件zTree实现删除树子节点的方法示例

程序员文章站 2022-10-14 22:22:37
本文实例讲述了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:131, pid:13, name:"黄州区"},
      { id:132, pid:13, name:"麻城市"},
      { id:133, pid:13, name:"武穴市"},
      { id:134, pid:13, name:"团风县"},
      { id:135, pid:13, name:"浠水县"},
      { id:136, pid:13, name:"罗田县"},
      { id:137, pid:13, 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 removenodes()
    {
      var treeobj = $.fn.ztree.getztreeobj("basetree");
      //选中节点
      var nodes = treeobj.getselectednodes();
      for (var i=0, l=nodes.length; i < l; i++)
      {
        //删除选中的子节点
        treeobj.removechildnodes(nodes[i]);
      }
    }
    //-->
  </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="removenodes()" value="删除子节点"/>
  </div>
</div>
</body>
</html>

2、实现效果图:

(1)初始化

jQuery插件zTree实现删除树子节点的方法示例

(2)选择“武汉”,然后单击“删除子节点”

jQuery插件zTree实现删除树子节点的方法示例

(3)选择“武汉市”,然后单击“删除子节点”,不能再删除子节点

jQuery插件zTree实现删除树子节点的方法示例

3、源码说明

//删除选中的子节点
treeobj.removechildnodes(nodes[i]);

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

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

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