您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页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>

804b5370a7cbc65aee99e223b7fd9.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>

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

6d0916c6f929f9a922b993f48a9ae.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框架教程栏目进行学习!

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务