search
HomeWeb Front-endLayui TutorialHow to clear the layui tree

How to clear the layui tree

Jul 30, 2019 pm 01:46 PM
layui

How to clear the layui tree

How to clear the layui tree

First create a tree box:

<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>

How to clear the layui tree

<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([&#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>

How to clear the layui tree

Add branches to the original trunk:

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
           
          }
        ]
      }
      
    ]
  });

How to clear the layui tree

Add branches based on the previous ones:

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
              }
            ]
          }
        ]
      }
      
    ]
  });

How to clear the layui tree

Add leaves based on the previous one:

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
              }
            ]
          }
        ]
      }
      
    ]
  });

How to clear the layui tree

Add a clear button:

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

How to clear the layui tree

Click the clear button and call the click event to clear the tree

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

How to clear the layui tree

Method/Step 2

Complete code :




  
  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>

    For more technical articles related to Layui, please visit the Layui Framework Tutorial column to learn!

    The above is the detailed content of How to clear the layui tree. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor