JS infinitus tree menu, general examples of json format and array format

Source: Internet
Author: User

Modified the data format. json and array or hybrid data are both common and do not use specific keys.
Copy codeThe Code is as follows:
<! 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 tree tree-like menu, json format, and common array format </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
{
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 = "Expand All"/>
<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": "Gourmet", "mvonline": [9, 8, [9, 8, 7, 6, 5, 4], 6, 5, 4], "district_online": "1", "zone_online": "1", "subway_online": "1", "city_online ": "1 "};
/* Recursively retrieve the stepless tree data and generate the DOM structure */
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;
}
/* Add a stepless tree */
Document. getElementById ("menuTree"). innerHTML = forTree (json );
/* Tree menu */
Var menuTree = function (){
// Add [+-] to the element with sub-objects
$ ("# MenuTree ul"). each (function (index, element ){
Var ulContent = contents (element).html ();
Var spanContent = $ (element). siblings ("span" pai.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 );
Certificate (this).html ("[-]" + spanContent );
} Else {
Ul. hide (300 );
Certificate (this).html ("[+]" + spanContent );
}
}
})
}()

/* Expand */
$ ("# Btn_open"). click (function (){
$ ("# MenuTree ul"). show (300 );
Curzt ("-");
})
/* Contract */
$ ("# 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 ){
Certificate (this).html ("[" + v + "]" + spanContent );
}
});
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.