• 技术文章 >web前端 >Layui教程

    layui树怎么清空

    藏色散人藏色散人2019-08-01 16:35:31原创2195

    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="margin-top: 20px;">
      <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

    完整代码:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>layui</title>
      <meta name="renderer" content="webkit">
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
      <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">
      <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
    </head>
    <body>
                
    <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;">
      <ul id="demo1"></ul>
    </div>
     
    <button class="layui-btn">清空</button>
               
              
    <script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
    <!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->
    <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
                  }
                ]
              }
            ]
          }
          
        ]
      });
      
    $(".layui-btn").click(function(){
    $('ul li').remove();
    });
      
    });
    </script>
    </body>
    </html>

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

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

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:layui
    上一篇:layui下拉选中的值怎么回填 下一篇:layui和后端如何连起来
    大前端线上培训班

    相关文章推荐

    • layui后端怎么搭建• layui如何提交post• layui弹窗怎么用• layui为什么好

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网