廢話不多說(shuō),直接上代碼吧!
<!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 href="layui/css/layui.css" rel="external nofollow" rel="stylesheet" /> <script src="layui/layui.js"></script> <!-- 注意:如果你直接復(fù)制所有代碼到本地,上述css路徑需要改成你本地的 --> </head> <body> <div class="layui-layout layui-layout-admin"> <div class="layui-header"> <div class="layui-logo">layui 后臺(tái)布局</div> <!-- 頭部區(qū)域(可配合layui已有的水平導(dǎo)航) --> <ul class="layui-nav layui-layout-left"> <li class="layui-nav-item"><a href="">控制臺(tái)</a></li> <li class="layui-nav-item"><a href="">商品管理</a></li> <li class="layui-nav-item"><a href="">用戶</a></li> <li class="layui-nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >其它系統(tǒng)</a> <dl class="layui-nav-child"> <dd><a href="">郵件管理</a></dd> <dd><a href="">消息管理</a></dd> <dd><a href="">授權(quán)管理</a></dd> </dl> </li> </ul> <ul class="layui-nav layui-layout-right"> <li class="layui-nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img src="http://t.cn/RCzsdCq" class="layui-nav-img"> 賢心 </a> <dl class="layui-nav-child"> <dd><a href="">基本資料</a></dd> <dd><a href="">安全設(shè)置</a></dd> </dl> </li> <li class="layui-nav-item"><a href="">退了</a></li> </ul> </div> <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"> <li class="layui-nav-item layui-nav-itemed"> <a class="" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >所有商品</a> <dl class="layui-nav-child"> <dd><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >列表一</a></dd> <dd><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >列表二</a></dd> <dd><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >列表三</a></dd> <dd><a href="">超鏈接</a></dd> </dl> </li> <li class="layui-nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >解決方案</a> <dl class="layui-nav-child"> <dd><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >列表一</a></dd> <dd><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >列表二</a></dd> <dd><a href="">超鏈接</a></dd> </dl> </li> <li class="layui-nav-item"><a href="">云市場(chǎng)</a></li> <li class="layui-nav-item"><a href="">發(fā)布商品</a></li> </ul> </div> </div> <div class="layui-body"> <div style="padding: 3px;"> <div class="layui-tab" lay-filter="demo" > <ul class="layui-tab-title" > <li class="layui-this" lay-id="11" id="11">網(wǎng)站設(shè)置 </li> <li lay-id="22">用戶管理 <i class="layui-icon layui-unselect layui-tab-close">ဆ</i></li> <li lay-id="33">權(quán)限分配</li> <li lay-id="44">商品管理</li> <li lay-id="55">訂單管理</li> </ul> <div class="layui-tab-content"> <div class="layui-tab-item layui-show">內(nèi)容1</div> <div class="layui-tab-item">內(nèi)容2</div> <div class="layui-tab-item">內(nèi)容3</div> <div class="layui-tab-item">內(nèi)容4</div> <div class="layui-tab-item">內(nèi)容5</div> </div> </div> <div class="site-demo-button" style="margin-bottom: 0;"> <button class="layui-btn site-demo-active" data-type="tabAdd">新增Tab項(xiàng)</button> <button class="layui-btn site-demo-active" data-type="tabDelete">刪除:商品管理</button> <button class="layui-btn site-demo-active" data-type="tabChange">切換到:用戶管理</button> </div> </div> </div> </div> <!-- 注意:如果你直接復(fù)制所有代碼到本地,上述js路徑需要改成你本地的 --> <script> layui.use('element', function () { var $ = layui.jquery , element = layui.element; //Tab的切換功能,切換事件監(jiān)聽等,需要依賴element模塊 //觸發(fā)事件 var active = { tabAdd: function () { //新增一個(gè)Tab項(xiàng) element.tabAdd('demo', { title: '新選項(xiàng)<i class="layui-icon layui-unselect layui-tab-close">ဆ</i>' //用于演示 , content: '內(nèi)容' + (Math.random() * 1000 | 0) , id: new Date().getTime() //實(shí)際使用一般是規(guī)定好的id,這里以時(shí)間戳模擬下 }); //增加點(diǎn)擊關(guān)閉事件 var r = $(".layui-tab-title").find("li"); //每次新打開tab都是最后一個(gè),所以只對(duì)最后一個(gè)tab添加點(diǎn)擊關(guān)閉事件 r.eq(r.length - 1).children("i").on("click", function () { alert($(this).parent("li").attr('lay-id')); element.tabDelete("demo", $(this).parent("li").attr('lay-id')); }), element.tabChange("demo", r.length - 1); element.init(); } , tabDelete: function (othis) { //刪除指定Tab項(xiàng) element.tabDelete('demo', '44'); //刪除:“商品管理” othis.addClass('layui-btn-disabled'); } , tabChange: function () { //切換到指定Tab項(xiàng) element.tabChange('demo', '22'); //切換到:用戶管理 } }; $('.site-demo-active').on('click', function () { var othis = $(this), type = othis.data('type'); active[type] ? active[type].call(this, othis) : ''; }); //Hash地址的定位 var layid = location.hash.replace(/^#test=/, ''); element.tabChange('test', layid); element.on('tab(test)', function (elem) { location.hash = 'test=' + $(this).attr('lay-id'); }); }); </script> </body> </html>
以上這篇layerui代碼控制tab選項(xiàng)卡,添加,關(guān)閉的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com