P粉3814637802023-08-14 12:26:26
Here is a quick code snippet that replicates your problem. Run it and then make a check element on the checkbox. Notice that checking/unchecking the checkbox doesn't change the HTML code you're checking?
"use strict"; window.addEventListener('load', onLoaded, false); function onLoaded(evt) { document.getElementById('addNewBtn').addEventListener('click', onAddBtnClick, false); } function onAddBtnClick(evt) { let newStr = "<li><input type='checkbox'/></li>"; document.querySelector('ul').innerHTML += newStr; }
<ul> <li><input type='checkbox'/></li> </ul> <button id='addNewBtn'>添加新项目</button>
This is an alternative code snippet that adds an item to the end of the list.
"use strict"; window.addEventListener('load', onLoaded, false); function onLoaded(evt) { document.getElementById('addNewBtn').addEventListener('click', onAddBtnClick2, false); } function onAddBtnClick2(evt) { let newLi = document.createElement('li'); newLi.innerHTML = "<input type='checkbox'/>"; document.querySelector('ul').appendChild(newLi); }
<ul> <li><input type='checkbox'/></li> </ul> <button id='addNewBtn'>添加新项目</button>