Topic:

2. Use native JS, the last p tag in UL Li. (1) Delete the last p label (2) Click any Li label to print the serial number. (with direct binding and event delegation)

The answer:

<ul id="oBox"> <li> <p>1</p> <p>2</p> </li> <li> <p>1</p> <p>2</p> </li> <li> <p>1</p> <p>2</p> </li> </ul> <script> let  oUl = document.getElementById('oBox'); let oLi = oUl.getElementsByTagName('li'); For (let I = 0; i < oLi.length; i ++) { var lastChild = oLi[i].lastElementChild ? oLi[i].lastElementChild : oLi[i].lastChild; OLi [I]. RemoveChild (lastChild)} for(let I = 0; i < oLi.length; Onclick = function() {alert(I)}} oUl. AddEventListener ("click",clickHandler); function clickHandler(e) { if(e.target.nodeName.toLowerCase() ! == 'li') return; // Or write if(e.trap.constructor! ==HTMLLIElement) return; let arr = Array.from(oUl.children) let index = arr.indexOf(e.target); alert(index) } </script>Copy the code