Infinite tree menu effect code implemented by json jQuery_jquery
The example in this article describes the infinite tree menu effect code implemented by json jQuery. Share it with everyone for your reference. The details are as follows:
Here is a demonstration of json tree menu, JS stepless tree menu, which introduces the jQuery plug-in and uses recursive implementation to obtain stepless tree data and generate a DOM structure. You can expand infinite levels in JSON data. You can understand it by looking at the structure.
Let’s take a look at the screenshots of the running effect:
The online demo address is as follows:
http://demo.jb51.net/js/2015/jquery-json-tree-style-menu-codes/
The specific 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="jquery-1.6.2.min.js"></script> <title>JS无级树树形菜单</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 = [{"name":"*a","list":[ {"name":"**a","url":"#a1"}, {"name":"**aa","list":[ {"name":"***a","url":"#a11"}, {"name":"***aa","list":[ {"name":"****a","url":"#a111"}, {"name":"****aa","list":[ {"name":"*****a","url":"#a1111"}, {"name":"*****aa","url":"#a1112"} ]} ]}, {"name":"***aaa","url":"#a13"}, {"name":"***aaaa","url":"#a14"} ] }, {"name":"**a","url":"#a3"} ] }, {"name":"*b","list":[ {"name":"**b","url":"#b1"}, {"name":"**bb","list":[ {"name":"****b","url":"#b111"}, {"name":"****bb","url":"#b112"} ] }, ] }, {"name":"*c","list":[ {"name":"**c","url":"#c1"}, {"name":"**cc","url":"#c2"} ] }, {"name":"*d"} ] /*递归实现获取无级树数据并生成DOM结构*/ var str = ""; var forTree = function(o){ for(var i=0;i<o.length;i++){ var urlstr = ""; try{ if(typeof o[i]["url"] == "undefined"){ urlstr = "<div><span>"+ o[i]["name"] +"</span><ul>"; }else{ urlstr = "<div><span><a href="+ o[i]["url"] +">"+ o[i]["name"] +"</a></span><ul>"; } str += urlstr; if(o[i]["list"] != null){ forTree(o[i]["list"]); } str += "</ul></div>"; }catch(e){} } 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>
I hope this article will be helpful to everyone’s jquery programming design.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
