layui添加動態(tài)菜單與選項卡
更新時間:2019年07月26日 14:15:06 作者:探子
這篇文章主要為大家詳細(xì)介紹了layui添加動態(tài)菜單與選項卡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了layui添加動態(tài)菜單與選項卡的具體代碼,供大家參考,具體內(nèi)容如下
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>Layui</title> <link rel="stylesheet" href="js/css/layui.css" media="all"> </head> <body> <div class="layui-side layui-bg-black"> <div class="layui-side-scroll"> <!-- 左側(cè)導(dǎo)航區(qū)域(可配合layui已有的垂直導(dǎo)航) --> <ul class="layui-nav layui-nav-tree" lay-filter="test" id="memus"></ul> </div> </div> <div class="layui-body"> <!-- 動態(tài)選項卡 --> <div id="tabzu" class="layui-tab layui-tab-card layui-tab-brief" lay-filter="tabDemo" lay-allowclose="true"> <ul class="layui-tab-title"></ul> <div class="layui-tab-content"></div> </div> </div> <script src="js/layui.all.js" charset="utf-8"></script> <script type="text/javascript" src="js/index.js" ></script> </body> </html>
index.js
layui.use('element', function() {
function checkLastItem(arr, i) {
return arr.length == (i + 1);
}
function getAhtml(obj){
return "<a href=\"javascript:;\" οnclick=\"addTab('" + obj.name + "','" + obj.url + "')\" >" + obj.name + "</a>";
}
//動態(tài)菜單
layui.jquery.ajax({
url: "http://127.0.0.1:18000/sys/menus",
method: 'POST',
success: function(res) {
var html = "";
for(var i = 0; i < res.length; i++) {
var strli = "<li class=\"layui-nav-item lay-unselect \" >";
if (res[i].url =='#'){
strli = strli + "<a href=\"javascript:;\">" + res[i].name + "</a>";
console.log(res[i].name)
}else{
strli = strli + getAhtml(res[i]);
}
if(res[i].pId == "0" && !checkLastItem(res, i) && res[i + 1].pId != "0") {
strli = strli + "<dl class=\"layui-nav-child\" >";
for(; !checkLastItem(res, i) && res[i + 1].pId != "0"; i++) {
strli = strli + "<dd>"+getAhtml(res[i+1])+"</dd>";
}
strli = strli + "</dl>";
}
strli = strli + "</li>";
html = html + strli;
}
layui.jquery("#memus").html(html);
layui.element.init(); //一定初始化一次
}
})
});
//添加選項卡
function addTab(name, url) {
if(layui.jquery(".layui-tab-title li[lay-id='" + name + "']").length > 0) {
//選項卡已經(jīng)存在
layui.element.tabChange('tabDemo', name);
layer.msg('切換-' + name)
} else {
//動態(tài)控制iframe高度
var tabheight = layui.jquery(window).height() - 95;
contentTxt = '<iframe src="' + url + '" scrolling="no" width="100%" height="' + (tabheight) + 'PX"></iframe>';
//新增一個Tab項
layui.element.tabAdd('tabDemo', {
title: name,
content: contentTxt,
id: name
})
//切換刷新
layui.element.tabChange('tabDemo', name)
layer.msg('新增-' + name)
}
}
菜單JSON
[
{
"name": "首頁",
"url": "shouye.html",
"id": "1",
"pId": "0"
},
{
"name": "數(shù)據(jù)庫",
"url": "#",
"id": "1",
"pId": "0"
},
{
"name": "MYSQL",
"url": "mysql.html",
"id": "2",
"pId": "1"
},
{
"name": "ORACLE",
"url": "oracle.html",
"id": "3",
"pId": "1"
},
{
"name": "開發(fā)語言",
"url": "#",
"id": "4",
"pId": "0"
},
{
"name": "JAVA",
"url": "java.html",
"id": "5",
"pId": "4"
},
{
"name": "Python",
"url": "python.html",
"id": "6",
"pId": "4"
}
]
效果截圖:

如果大家還想深入學(xué)習(xí),可以點擊兩個精彩的專題:javascript選項卡操作方法匯總 jquery選項卡操作方法匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過js簡單實現(xiàn)將一個文本內(nèi)容轉(zhuǎn)譯成加密文本
將文本內(nèi)容轉(zhuǎn)譯成加密文本,在某些情況下還是比較實用的,下面通過js簡單實現(xiàn)下,感興趣的朋友不要錯過2013-10-10
javascript 函數(shù)參數(shù)原來是可以有缺省值的
前幾天看 javaEye 時看到一個童鞋寫的 getElementsByClassName 函數(shù)。2010-07-07
代碼短小的js div層拖動實現(xiàn)代碼[兼容IE與Firefox]
代碼短小的js div層拖動實現(xiàn)代碼[兼容IE與Firefox],需要的朋友可以參考下.2010-05-05
JavaScript顯示表單內(nèi)元素數(shù)量的方法
這篇文章主要介紹了JavaScript顯示表單內(nèi)元素數(shù)量的方法,涉及javascript操作表單屬性的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

