铁雪资源网 Design By www.gsvan.com
修改了一下数据格式,是json和数组或者混合型的数据都通用,不用特定key等
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.2.min.js"><title>JS无级树树形菜单,json格式,数组格式通用</title>
<style type="text/css">
.menuTree
{
margin-left: -30px;
}
.menuTree div
{
padding-left: 30px;
}
.menuTree div ul
{
overflow: hidden;
display: none;
height: auto;
}
.menuTree span
{
display: block;
height: 25px;
line-height: 25px;
padding-left: 5px;
margin: 1px 0;
cursor: pointer;
border-bottom: 1px solid #CCC;
}
.menuTree span:hover
{
background-color: #e6e6e6;
color: #cf0404;
}
.menuTree a
{
color: #333;
text-decoration: none;
}
.menuTree a:hover
{
color: #06F;
}
.btn
{
height: 30px;
margin-top: 10px;
border-bottom: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="btn">
<input name="" type="button" id="btn_open" value="全部展开" />
<input name="" type="button" id="btn_close" value="全部收缩" />
</div>
<div id="menuTree" class="menuTree">
</div>
</body>
</html>
<script type="text/javascript">
var json = { "navnums": { "0": "8051", "4": "4969", "8": "206", "5": "126", "9": "2174" }, "hotwords": "美食", "mvonline": [9, 8, [9, 8, 7, 6, 5, 4], 6, 5, 4], "district_online": "1", "zone_online": "1", "subway_online": "1", "city_online": "1" };
/*递归实现获取无级树数据并生成DOM结构*/
var str = "";
var forTree = function (o) {
var urlstr = "";
var keys = new Array();
for (var key in o) {
keys.push(key);
}
for (var j = 0; j < keys.length; j++) {
k = keys[j];
if (typeof o[k] == "object") {
urlstr = "<div><span>" + k + "</span><ul>";
} else {
urlstr = "<div><span>" + k + "=" + o[k] + "</span><ul>";
}
str += urlstr;
var kcn = 0;
if (typeof o[k] == "object") {
for (var kc in o[k]) {
kcn++;
}
}
if (kcn > 0) {
forTree(o[k]);
}
str += "</ul></div>";
}
return str;
}
/*添加无级树*/
document.getElementById("menuTree").innerHTML = forTree(json);
/*树形菜单*/
var menuTree = function () {
//给有子对象的元素加[+-]
$("#menuTree ul").each(function (index, element) {
var ulContent = $(element).html();
var spanContent = $(element).siblings("span").html();
if (ulContent) {
$(element).siblings("span").html("[+] " + spanContent)
}
});
$("#menuTree").find("div span").click(function () {
var ul = $(this).siblings("ul");
var spanStr = $(this).html();
var spanContent = spanStr.substr(3, spanStr.length);
if (ul.find("div").html() != null) {
if (ul.css("display") == "none") {
ul.show(300);
$(this).html("[-] " + spanContent);
} else {
ul.hide(300);
$(this).html("[+] " + spanContent);
}
}
})
} ()
/*展开*/
$("#btn_open").click(function () {
$("#menuTree ul").show(300);
curzt("-");
})
/*收缩*/
$("#btn_close").click(function () {
$("#menuTree ul").hide(300);
curzt("+");
})
function curzt(v) {
$("#menuTree span").each(function (index, element) {
var ul = $(this).siblings("ul");
var spanStr = $(this).html();
var spanContent = spanStr.substr(3, spanStr.length);
if (ul.find("div").html() != null) {
$(this).html("[" + v + "] " + spanContent);
}
});
}
</script>
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.2.min.js"><title>JS无级树树形菜单,json格式,数组格式通用</title>
<style type="text/css">
.menuTree
{
margin-left: -30px;
}
.menuTree div
{
padding-left: 30px;
}
.menuTree div ul
{
overflow: hidden;
display: none;
height: auto;
}
.menuTree span
{
display: block;
height: 25px;
line-height: 25px;
padding-left: 5px;
margin: 1px 0;
cursor: pointer;
border-bottom: 1px solid #CCC;
}
.menuTree span:hover
{
background-color: #e6e6e6;
color: #cf0404;
}
.menuTree a
{
color: #333;
text-decoration: none;
}
.menuTree a:hover
{
color: #06F;
}
.btn
{
height: 30px;
margin-top: 10px;
border-bottom: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="btn">
<input name="" type="button" id="btn_open" value="全部展开" />
<input name="" type="button" id="btn_close" value="全部收缩" />
</div>
<div id="menuTree" class="menuTree">
</div>
</body>
</html>
<script type="text/javascript">
var json = { "navnums": { "0": "8051", "4": "4969", "8": "206", "5": "126", "9": "2174" }, "hotwords": "美食", "mvonline": [9, 8, [9, 8, 7, 6, 5, 4], 6, 5, 4], "district_online": "1", "zone_online": "1", "subway_online": "1", "city_online": "1" };
/*递归实现获取无级树数据并生成DOM结构*/
var str = "";
var forTree = function (o) {
var urlstr = "";
var keys = new Array();
for (var key in o) {
keys.push(key);
}
for (var j = 0; j < keys.length; j++) {
k = keys[j];
if (typeof o[k] == "object") {
urlstr = "<div><span>" + k + "</span><ul>";
} else {
urlstr = "<div><span>" + k + "=" + o[k] + "</span><ul>";
}
str += urlstr;
var kcn = 0;
if (typeof o[k] == "object") {
for (var kc in o[k]) {
kcn++;
}
}
if (kcn > 0) {
forTree(o[k]);
}
str += "</ul></div>";
}
return str;
}
/*添加无级树*/
document.getElementById("menuTree").innerHTML = forTree(json);
/*树形菜单*/
var menuTree = function () {
//给有子对象的元素加[+-]
$("#menuTree ul").each(function (index, element) {
var ulContent = $(element).html();
var spanContent = $(element).siblings("span").html();
if (ulContent) {
$(element).siblings("span").html("[+] " + spanContent)
}
});
$("#menuTree").find("div span").click(function () {
var ul = $(this).siblings("ul");
var spanStr = $(this).html();
var spanContent = spanStr.substr(3, spanStr.length);
if (ul.find("div").html() != null) {
if (ul.css("display") == "none") {
ul.show(300);
$(this).html("[-] " + spanContent);
} else {
ul.hide(300);
$(this).html("[+] " + spanContent);
}
}
})
} ()
/*展开*/
$("#btn_open").click(function () {
$("#menuTree ul").show(300);
curzt("-");
})
/*收缩*/
$("#btn_close").click(function () {
$("#menuTree ul").hide(300);
curzt("+");
})
function curzt(v) {
$("#menuTree span").each(function (index, element) {
var ul = $(this).siblings("ul");
var spanStr = $(this).html();
var spanContent = spanStr.substr(3, spanStr.length);
if (ul.find("div").html() != null) {
$(this).html("[" + v + "] " + spanContent);
}
});
}
</script>
铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com
暂无JS无限极树形菜单,json格式、数组格式通用示例的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。