我有这样一个JavaScript
const checkBox = document.createElement("input")
checkBox.className = "form-check-input me-1 fa-pull-right"
checkBox.type = "checkbox"
li.appendChild(checkBox);
checkBox.id = "checkBox";
checkBox.addEventListener('change', function(e) {
if (checkBox.checked) {
li.setAttribute = ("id", "completed");
} else
li.removeAttribute = ("id", "completed");
});
创建输入的代码,并在检查时添加样式 id 标记以添加行,但我收到此错误。
未捕获类型错误:无法读取 null 的属性(读取“addEventListener”)”
for (let i = 1; i <= 1; i++) {
const checkBox = document.createElement("input")
checkBox.className = "form-check-input me-1 fa-pull-right";
checkBox.type = "checkbox"
checkBox.id = "checkBox";
document.body.appendChild(checkBox);
};
parent.addEventListener('change', (event) => {
const element = event.target;
if (element.id === 'checkBox' && element.checked) {
element.id = 'completed'
} else {
element.id = ''
}
});
希望有帮助!