大家好,又见面了,我是你们的朋友全栈君。
刚开始学习layui框架,碰到一个缺陷是二级菜单每次点击都会收缩到原始状态。用 Request.Url.ToString();获取URL判断点击页面前哪些节点是展开的,添加class=\”layui-nav-item layui-nav-itemed\”>(展开子节点)解决了。代码如下:
for (var i = 0; i < data.length; i++) {
//添加头栏目
if (data[i].children.length > 0) {
var curFocus = null;
var curUrl = '@Request.Url.ToString()';
var hasFocusParent = null;
var clickTree = null;
for (var j = 0; j < data[i].children.length; j++) {
if (curUrl.lastIndexOf(data[i].children[j].href) > 0) {
clickTree = data[i].children[j].id;
hasFocusParent = data[i].id;
}
};
if (hasFocusParent == data[i].id) {
item += "<li class=\"layui-nav-item layui-nav-itemed\"> <a href=\"javascript:;\">" + data[i].name + "</a><dl class=\"layui-nav-child\">";
//class=\"layui-nav-item layui-nav-itemed\">展开子节点
}
else {
item += "<li class=\"layui-nav-item\"><a href=\"javascript:;\">" + data[i].name + "</a><dl class=\"layui-nav-child\">";
}
for (var j = 0; j < data[i].children.length; j++) {
if (data[i].children[j].id == clickTree) {
item += "<dd style=\"background-color: #009688\"><a href=\"" + data[i].children[j].href + "\">" + data[i].children[j].name + "</a></dd>";
} else {
item += "<dd><a href=\"" + data[i].children[j].href + "\">" + data[i].children[j].name + "</a></dd>";
}
};
item += "</dl></li>";
} else {
item += "<li class=\"layui-nav-item\"><a href=\"" + data[i].href + "\">" + data[i].name + "</a></li>";
}
};
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/146209.html原文链接:https://javaforall.cn