js操作页面cookie以及亮灯
var active = {}; var name = 'add_album'; LightUp(name); $('#au_screen .layui-btn').on('click', function () { var othis = $(this), method = othis.data('method'); active[method] ? active[method].call(this, othis) : ''; var screen = Cookies.get(name, {path: ''}) ? JSON.parse(Cookies.get(name, {path: ''})) : []; screen.includes(method) ? screen.splice(screen.indexOf(method), 1) : screen.push(method); Cookies.set(name, JSON.stringify(screen), {path: ''}); LightUp(name); }); function LightUp(name) { var screen = $('#au_screen').find('.layui-btn'); for (let index = 0; index < screen.length; index++) { const element = screen[index]; $('#' + element.id).addClass("layui-btn-primary"); } var screen = Cookies.get(name) ? JSON.parse(Cookies.get(name)) : []; for (let index = 0; index < screen.length; index++) { var element = screen[index]; $('#' + element).removeClass("layui-btn-primary"); } }
<script src="https://aus.shejiz.cn/ajax/libs/js-cookie/3.0.2/js.cookie.min.js" charset="utf-8"></script>
<div class="layui-btn-container" id="au_screen"> {foreach $album as $k=>$v} <button class="layui-btn layui-btn-sm layui-btn-primary" data-method="{$k}" id="{$k}">{$v}</button> {/foreach} </div>