搜索
首页web前端Layui教程layui树怎么清空

layui树怎么清空

Jul 30, 2019 pm 01:46 PM
layui

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="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([&#39;tree&#39;, &#39;layer&#39;], function(){
  var layer = layui.layer
  ,$ = layui.jquery; 
  
  layui.tree({
    elem: &#39;#demo1&#39; //指定元素
    ,target: &#39;_blank&#39; //是否新选项卡打开(比如节点返回href才有效)
    ,click: function(item){ //点击节点回调
      layer.msg(&#39;当前节名称:&#39;+ item.name + &#39;<br>全部参数:&#39;+ JSON.stringify(item));
      console.log(item);
    }
    ,nodes: [ //节点
      {
        name: &#39;树干&#39;
        ,id: 2
        ,spread: true
 }
        ]
  });
});
</script>

7c409ee5ec1bc3d72b164254fd8cd36.png

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

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

0ed27c9df941d01282a0c306d96deba.png

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

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

947e594a18e059d5b7080ad47c14160.png

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

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

96bf9933a6b8e553bcb26e280d743d6.png

添加个清空的按钮:

<button class="layui-btn">清空</button>

6d0916c6f929f9a922b993f48a649ae.png

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

$(".layui-btn").click(function(){
$(&#39;ul li&#39;).remove();
});

655a1aa9253bece6bf61951adc39537.png

方法/步骤2

完整代码:




  
  layui
  
  
  
  
  


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

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

    以上是layui树怎么清空的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热工具

    mPDF

    mPDF

    mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

    SublimeText3 英文版

    SublimeText3 英文版

    推荐:为Win版本,支持代码提示!

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    Dreamweaver Mac版

    Dreamweaver Mac版

    视觉化网页开发工具

    VSCode Windows 64位 下载

    VSCode Windows 64位 下载

    微软推出的免费、功能强大的一款IDE编辑器