gpt4 book ai didi

javascript - Null in not an object(评估元素Text.style)

转载 作者:太空宇宙 更新时间:2023-11-04 02:35:08 25 4
gpt4 key购买 nike

在待办事项列表中,我试图删除一个已勾选的元素,但我收到错误消息 - “Null in not an object (evaluating 'itemText.style')”

任何人都可以解释我应该如何改变它以使删除线工作吗?如果可能的话,我还尽量避免将 CSS 放入我的 HTML 文件中。

function removeItem() {
var boxId = this.id.replace("boxId_", "");
var itemText = document.getElementById("item_", + boxId);
itemText.style.setProperty("text-decoration", "line-through"); //error here
}

function addNewItem(list, itemText) {
totalItems++;

var listItem = document.createElement("li");
var checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.id = "cb_" + totalItems;

var span = document.createElement("span");
span.id = "item_" + totalItems;
span.innerText = itemText;
checkBox.onclick = removeItem;

listItem.appendChild(checkBox);
listItem.appendChild(span);
list.appendChild(listItem);
}

var btnNew = document.getElementById("btnAdd");

var totalItems = 0;
var inItemText = document.getElementById("inItemText");
inItemText.focus();

btnNew.onclick = function() {
var itemText = inItemText.value;

if (!itemText || itemText == "") {
return false;
}

addNewItem(document.getElementById("todoList"), itemText);
};

最佳答案

就是因为这条线

var itemText = document.getElementById("item_", + boxId);

结束引号后有一个逗号。您的代码基本上忽略了 boxId,因为它被用作 getElementById 忽略的另一个参数。

成功了

var itemText = document.getElementById("item_" + boxId); 

关于javascript - Null in not an object(评估元素Text.style),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35837321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com