JS無限極樹形選單,json格式、陣列格式通用示例

NO IMAGE

修改了一下資料格式,是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=”http://d1.lashouimg.com/static/js/release/jquery-1.4.2.min.js”></script>
<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>

您可能感興趣的文章:

jquery ztree實現下拉樹形框使用到了json資料json jQuery實現的無限級樹形選單效果程式碼基於jsTree的無限級樹JSON資料的轉換程式碼利用Dojo和JSON建立無限級AJAX動態載入的功能模組樹基於jQuery的樹控制元件實現程式碼(asp.net json)jquery使用EasyUI Tree非同步載入JSON資料(生成樹)基於MVC5和Bootstrap的jQuery TreeView樹形控制元件(一)之資料支援json字串、list集合JSON複雜資料處理之Json樹形結構資料轉Java物件並儲存到資料庫的實現基於MVC5和Bootstrap的jQuery TreeView樹形控制元件(二)之資料支援json字串、list集合基於jstree使用JSON資料組裝成樹