angular.js - 通過數(shù)據(jù)中children的個數(shù)自動生成能點擊展開的div
問題描述
數(shù)據(jù)如圖:
根據(jù)獲取的這些數(shù)據(jù)要做出這樣的效果:
最外面的父級一直都顯示,點擊后顯示它的子級,如果子級還有子級,點擊后又可以展開...
問題解答
回答1:<!DOCTYPE html> <html> <head><meta charset='utf-8' /><style type='text/css'> .list {margin: 0;padding-left: 20px; }.list li {list-style: none; }.hide {display: none; }.control ~ .control-label {display: inline-block;width: 0;height: 0;border: 6px solid transparent;border-left-color: #000; }.control:checked ~ .control-label {border-left-color: transparent;border-top-color: #000; }.control ~ .list {display: none; }.control:checked ~ .list {display: block; }.control-label::after {content: attr(data-title);display: block;margin-top: -6px;margin-left: 0.5em;width: 10em;font-size: 12px;line-height: 12px; }</style> <head> <body><script> function createItem(data) {var id = (Math.random()).toString(16).slice(2); var p = document.createElement(’p’); p.className = ’content’;var input = document.createElement(’input’);input.type = ’checkbox’; input.className = ’hide control’; input.id = ’control-’ + id; var label = document.createElement(’label’); label.className = ’control-label’; label.setAttribute(’for’,’control-’ + id); label.dataset.title = ’[’ + data.Code + ’]’ + data.Name;p.appendChild(input);p.appendChild(label); return p; } function createTree(data) {var ul = document.createElement(’ul’); ul.className = ’list’; for(var i = 0,len = data.length; i < len; i++) { var li = document.createElement(’li’); var p = createItem(data[i]); li.appendChild(p); if(data[i].children && data[i].children.length > 0) {p.appendChild(createTree(data[i].children)); } ul.appendChild(li);} return ul; } var elems = createTree(data); document.body.appendChild(elems);</script> </body></html>
相關(guān)文章:
1. 如何設(shè)置一個無限循環(huán)并打破它。(Java線程)2. 網(wǎng)頁爬蟲 - 如何使用使用java抓取信息并制作一個排名系統(tǒng)?3. mysql - 在log日志中已知用戶的某一步操作,如何獲取其上一步操作?4. php對mysql提取數(shù)據(jù)那種速度更快5. shell - mysql更新錯誤6. javascript - sublime快鍵鍵問題7. javascript - immutable配合react提升性能?8. python小白 自學(xué)看書遇到看不懂的地方9. macos - 如何徹底刪除mac自帶的apache和php10. DADB.class.php文件的代碼怎么寫

網(wǎng)公網(wǎng)安備