javascript 靜態樹菜單實現代碼_導覽功能表

來源:互聯網
上載者:User
誰有興趣的話可以改成動態載入資料,或者用jquery,代碼肯定會少很多!我發現添加一些css,這棵樹在靜態頁面還是大有用途的!
<!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> <style type="text/css"> .expand{ cursor:pointer; } .hide{ cursor:pointer; } </style> <script type="text/javascript"> function toggleChild(o) { var cls = o.getAttribute("class"); if (cls == "expand") { var sb = o.nextSibling; if (window.innerWidth) sb = sb.nextSibling; while (sb != null && sb.tagName.toLowerCase() == 'dd') { sb.style.display = "none"; sb = sb.nextSibling; if (window.innerWidth) sb = sb.nextSibling; } o.removeAttribute("class"); o.setAttribute("class", "hide"); if (window.innerWidth) //ff o.textContent = "田" + o.textContent.substring(1); else o.innerText = "田" + o.innerText.substring(1); } else { var sb = o.nextSibling; if (window.innerWidth) sb = sb.nextSibling; while (sb != null && sb.tagName.toLowerCase() == 'dd') { sb.style.display = ""; sb = sb.nextSibling; if (window.innerWidth) sb = sb.nextSibling; } o.removeAttribute("class"); o.setAttribute("class", "expand"); if (window.innerWidth) //ff o.textContent = "曰" + o.textContent.substring(1); else o.innerText = "曰" + o.innerText.substring(1); } } function maketree(obj) { var dts = obj.getElementsByTagName('dt'); for (var i = 0; i < dts.length; i++) { dts[i].setAttribute("class", "expand"); if (window.innerWidth) //ff dts[i].textContent = "曰" + dts[i].textContent; else dts[i].innerText = "曰" + dts[i].innerText; dts[i].onclick = function() { toggleChild(this); }; } } window.onload = function() { maketree(document.getElementById("tree")); }; </script> </head> <body> <h3>定義列表也能變成一棵樹:</h3> <dl id="tree"> <dt>菜單1</dt> <dd>1.1 aaa</dd> <dd>1.2 bbb</dd> <dd>1.3 ccc</dd> <dt>菜單2</dt> <dd>2.1 你好</dd> <dd>超連結</dd> <dt>菜單3</dt> <dd> <dl > <dt>3.1</dt> <dd>百度新聞</dd> <dd>搜狐新聞</dd> <dt>3.2</dt> <dd>百度新聞</dd> <dd>搜狐新聞</dd> </dl> </dd> </dl> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.