0
我試圖建立一個簡單的待辦事項應用程序,我遇到了一個錯誤。
我得到一個輸入到輸入中的文本,將它分配給帶有innerText的項目,並在單擊該按鈕時將項目添加到我的列表中。
但是當我點擊按鈕,我得到文本沒有被定義傳入函數的值未定義?
我是小白在JavaScript和不知道爲什麼發生這種情況,以及如何解決它所以任何幫助,將不勝感激。
This is the link to codepen with my code
document.getElementById('todo').addEventListener('click', function(e) {
e.preventDefault();
var value = document.getElementById('input-field').value;
if (value) {
//console.log(value)
addItem(text)
}
});
function addItem(text) {
// reveal an input field
var action = 1;
if (action) {
var input = document.getElementById('input-field');
input.style.width = '0' ? '85%' : '0';
action = 0;
} else if (!action) {
//create element and add it to the DOM
var list = document.getElementById('list');
var item = document.createElement('li');
item.innerText = text;
var btn = document.createElement('button');
btn.classList.add('todo__btn');
item.appendChild(btn);
list.appendChild(item);
action = 1;
}
}
document.getElementById('add-item').addEventListener('click', addItem);
'的addItem(值);''未的addItem(文本);'。你的變量被稱爲「值」而不是「文本」。 –