1. // 循环创建10个li标签,当点击li标签时,打印其对应的索引
    2. <ul id="list"></ul>
    3. function loadNode(len) {
    4. var html = '';
    5. for (let index = 0; index < 10; index++) {
    6. html += '<li>'+index+'</li>';
    7. }
    8. var list = document.getElementById('list');
    9. list.onclick = function(event) {
    10. event = event || window.event;
    11. var target = event.target || event.srcElement;
    12. if(target.nodeName.toLowerCase() === 'li') {
    13. console.log(target.innerText);
    14. }
    15. }
    16. list.innerHTML = html;
    17. }
    18. loadNode();