layui树怎么清空

藏色散人
发布: 2019-07-30 13:46:33
原创
4016人浏览过

layui树怎么清空

layui树怎么清空

首先创建一个树框:

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>基本树</legend>
</fieldset>
 
<div style="display: inline-block; width: 180px; height: 210px; padding: 10px; border: 1px solid #ddd; overflow: auto;">
  
</div>
登录后复制

804b5370a7cbc65a89ee99e223b7fd9.png

<fieldset class="layui-elem-field layui-field-title"   style="max-width:90%">
  <legend>基本树</legend>
</fieldset>
 
<div style="display: inline-block; width: 180px; height: 210px; padding: 10px; border: 1px solid #ddd; overflow: auto;">
  <ul id="demo1"></ul>
</div>
<script>
//Demo
layui.use(['tree', 'layer'], function(){
  var layer = layui.layer
  ,$ = layui.jquery; 
  
  layui.tree({
    elem: '#demo1' //指定元素
    ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效)
    ,click: function(item){ //点击节点回调
      layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item));
      console.log(item);
    }
    ,nodes: [ //节点
      {
        name: '树干'
        ,id: 2
        ,spread: true
 }
        ]
  });
});
</script>
登录后复制

7c409ee5ec1bc3d72b164254fd8cd36.png

在原有的树干上添加树杈:

layui.use(['tree', 'layer'], function(){
  var layer = layui.layer
  ,$ = layui.jquery; 
  
  layui.tree({
    elem: '#demo1' //指定元素
    ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效)
    ,click: function(item){ //点击节点回调
      layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item));
      console.log(item);
    }
    ,nodes: [ //节点
      {
        name: '树干'
        ,id: 2
        ,spread: true
        ,children: [
          {
            name: '树杈1'
            ,id: 21
            ,spread: true
            
          }, {
            name: '树杈2'
            ,id: 22
           
          }
        ]
      }
      
    ]
  });
登录后复制

0ed27c9df941d01282a0c306d96deba.png

再在之前的基础上添加树枝:

layui.tree({
    elem: '#demo1' //指定元素
    ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效)
    ,click: function(item){ //点击节点回调
      layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item));
      console.log(item);
    }
    ,nodes: [ //节点
      {
        name: '树干'
        ,id: 2
        ,spread: true
        ,children: [
          {
            name: '树杈1'
            ,id: 21
            ,spread: true
            ,children: [
              {
                name: '树枝'
                ,id: 211
                
              }
            ]
          }, {
            name: '树杈2'
            ,id: 22
            ,children: [
              {
                name: '树枝'
                ,id: 221
              }
            ]
          }
        ]
      }
      
    ]
  });
登录后复制

947e594a18e059d5b7080ad47c14160.png

再在之前的基础上添加树叶:

layui.tree({
    elem: '#demo1' //指定元素
    ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效)
    ,click: function(item){ //点击节点回调
      layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item));
      console.log(item);
    }
    ,nodes: [ //节点
      {
        name: '树干'
        ,id: 2
        ,spread: true
        ,children: [
          {
            name: '树杈1'
            ,id: 21
            ,spread: true
            ,children: [
              {
                name: '树枝'
                ,id: 211
                
,children: [
                  {
                    name: '树叶1'
                    ,id: 2111
                  }, {
                    name: '树叶2'
                    ,id: 2112
                  }, {
                    name: '树叶3'
                    ,id: 2113
                  }
                ]
              }
            ]
          }, {
            name: '树杈2'
            ,id: 22
            ,children: [
              {
                name: '树枝'
                ,id: 221
              }
            ]
          }
        ]
      }
      
    ]
  });
登录后复制

96bf9933a6b8e553bcb26e280d743d6.png

添加个清空的按钮:

<button class="layui-btn">清空</button>
登录后复制

6d0916c6f929f9a922b993f48a649ae.png

点击清空按钮,调用点击事件清除树

$(".layui-btn").click(function(){
$('ul li').remove();
});
登录后复制

655a1aa9253bece6bf61951adc39537.png

方法/步骤2

完整代码:




  
  layui
  
  
  
  
  


            
基本树
    <button class="layui-btn">清空</button> <script> //Demo layui.use(['tree', 'layer'], function(){ var layer = layui.layer ,$ = layui.jquery; layui.tree({ elem: '#demo1' //指定元素 ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效) ,click: function(item){ //点击节点回调 layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item)); console.log(item); } ,nodes: [ //节点 { name: '树干' ,id: 2 ,spread: true ,children: [ { name: '树杈1' ,id: 21 ,spread: true ,children: [ { name: '树枝' ,id: 211 ,children: [ { name: '树叶1' ,id: 2111 }, { name: '树叶2' ,id: 2112 }, { name: '树叶3' ,id: 2113 } ] } ] }, { name: '树杈2' ,id: 22 ,children: [ { name: '树枝' ,id: 221 } ] } ] } ] }); $(&quot;.layui-btn&quot;).click(function(){ $('ul li').remove(); }); }); </script>
    登录后复制

    更多Layui相关技术文章,请访问Layui框架教程栏目进行学习!

    以上就是layui树怎么清空的详细内容,更多请关注php中文网其它相关文章!

    最佳 Windows 性能的顶级免费优化软件
    最佳 Windows 性能的顶级免费优化软件

    每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

    下载
    相关标签:
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号