2017-08-23 36 views
-1

我遇到了一个错误 - “未捕获的TypeError:无法执行'节点'上的'appendChild':参数1不是类型'Node'”在行113上。据我了解(我是初学者),问题是变量“shoppingListItem”不是一个节点,而是一个字符串。我怎样才能解决这个问题?字符串节点错误

var shoppingList = { 
 
    list: [{ 
 
    item: 'milk', 
 
    isBought: false, 
 
    itemCounter: 1 
 
    }, { 
 
    item: 'beer', 
 
    isBought: false, 
 
    itemCounter: 1 
 
    }, { 
 
    item: 'sugar', 
 
    isBought: false, 
 
    itemCounter: 1 
 
    }], 
 
    displayShoppingList: function() { 
 
    //debugger; 
 
    if (this.list.length > 0) { 
 
     for (var x = 0; x < this.list.length; x++) { 
 
     if (this.list[x].isBought === true) { 
 
      console.log('(x)' + this.list[x].item); 
 
     } else { 
 
      console.log('()' + this.list[x].item); 
 
     } 
 
     } 
 
    } else { 
 
     console.log('Your shopping list is empty'); 
 
    } 
 
    }, 
 
    addToShoppingList: function(item) { 
 
    this.list.push({ 
 
     item: item, 
 
     isBought: false, 
 
     itemCounter: 1 
 
    }); 
 
    this.displayShoppingList(); 
 
    }, 
 
    changeShoppingList: function(place, newItem) { 
 
    this.list[place].item = newItem; 
 
    this.displayShoppingList(); 
 
    }, 
 
    changeCounter: function(place, newCounter) { 
 
    this.list[place].itemCounter = newCounter; 
 
    this.displayShoppingList(); 
 
    }, 
 
    makeItemBought: function(place) { 
 
    this.list[place].isBought = !this.list[place].isBought; 
 
    this.displayShoppingList(); 
 
    }, 
 
    deleteFromShoppingList: function(place) { 
 
    this.list.splice(place, 1); 
 
    this.displayShoppingList(); 
 
    }, 
 
    toggleAllItems: function() { 
 
    var allItems = this.list.length; 
 
    var boughtItems = 0; 
 
    for (var y = 0; y < allItems; y++) { 
 
     if (this.list[y].isBought === true) { 
 
     boughtItems++; 
 
     } 
 
    } 
 
    if (boughtItems === allItems) { 
 
     for (var z = 0; z < allItems; z++) { 
 
     this.list[z].isBought = false; 
 
     } 
 
    } else { 
 
     for (var a = 0; a < this.list.length; a++) { 
 
     this.list[a].isBought = true; 
 
     } 
 
    } 
 
    this.displayShoppingList(); 
 
    } 
 
}; 
 
var handlers = { 
 
    showAll: function() { 
 
    shoppingList.displayShoppingList(); 
 
    }, 
 
    toggleAll: function() { 
 
    shoppingList.toggleAllItems(); 
 
    showOnScreen.displayShoppingList(); 
 
    }, 
 
    addToShoppingList: function() { 
 
    var addToShoppingListInput = document.getElementById('addToShoppingListText'); 
 
    shoppingList.addToShoppingList(addToShoppingListInput.value); 
 
    addToShoppingListInput.value = ""; 
 
    showOnScreen.displayShoppingList(); 
 
    }, 
 
    changeShoppingList: function() { 
 
    var changeShoppingListInputNumber = document.getElementById('changeShoppingListNumber'); 
 
    var changeShoppingListInputText = document.getElementById('changeShoppingListText'); 
 
    shoppingList.changeShoppingList(changeShoppingListInputNumber.valueAsNumber, changeShoppingListInputText.value); 
 
    changeShoppingListInputNumber.value = ""; 
 
    changeShoppingListInputText.value = ""; 
 
    showOnScreen.displayShoppingList(); 
 
    }, 
 

 
}; 
 

 
var showOnScreen = { 
 
    displayShoppingList: function() { 
 
    var shoppingUnorderedList = document.querySelector('ul'); 
 
    shoppingUnorderedList.innerHTML = ''; 
 
    for (var x = 0; x < shoppingList.list.length; x++) { 
 
     var shoppingListItem = document.createElement('li'); 
 
     var isBoughtDisplay = ''; 
 
     if (shoppingList.list[x].isBought === true) { 
 
     isBoughtDisplay = '(x)' + shoppingList.list[x].item + ' ' + shoppingList.list[x].itemCounter; 
 
     } else { 
 
     isBoughtDisplay = '()' + shoppingList.list[x].item + ' ' + shoppingList.list[x].itemCounter; 
 
     } 
 
     shoppingListItem.textContent = isBoughtDisplay; 
 

 
     shoppingListItem.appendChild(this.createDeleteButton); // error is here 
 

 
     shoppingUnorderedList.appendChild(shoppingListItem); 
 

 
    } 
 

 

 
    }, 
 
    createDeleteButton: function() { 
 
    var deleteButton = document.createElement('button'); 
 
    deleteButton.textContent = 'Delete item'; 
 
    deleteButton.className = 'deleteButtonClass'; 
 
    return deleteButton; 
 
    } 
 
}; 
 
//shoppingList.addToShoppingList('muffin'); 
 
//shoppingList.toggleAllItems(); 
 
//add multiple items at the same time - divide them by “,” and push one by one(?) 
 
//counter on each item - add ‘counter’ property to item/isBought, increase by one (tap) or manually by counter (how? - figure out!) 
 
//swipe movements and mobile devices adaptation (read docs)
<!DOCTYPE html> 
 
<html> 
 

 
<body> 
 
    <h1>Список покупок</h1> 
 
    <button onclick='showOnScreen.displayShoppingList()'>Show Shopping List</button> 
 
    <button onclick='handlers.toggleAll()'>Toggle all on/off</button> 
 
    <div> 
 
    <input type='text' id='addToShoppingListText'> 
 
    <button onclick='handlers.addToShoppingList()'>Add to shopping list</button> 
 
    </div> 
 
    <div> 
 
    <input type='number' id='changeShoppingListNumber'> 
 
    <input type='text' id='changeShoppingListText'> 
 
    <button onclick='handlers.changeShoppingList()'>Change Shopping List Item</button> 
 
    </div> 
 
    <div> 
 
    <input type='number' id='changeCounterPlace'> 
 
    <input type='number' id='changeCounterValue'> 
 
    <button onclick='handlers.changeCounter()'>Change number of items</button> 
 
    </div> 
 
    <ul> 
 

 
    </ul> 
 
    <script src="script.js"></script> 
 
</body> 
 

 
</html>

+1

不滥用代码块。阻止你添加没有代码块的jsfiddle链接的规则存在的原因 –

+0

我认为这个问题是因为你没有在第113行调用'createDeleteButton':'this.createDeleteButton'改为'this.createDeleteButton()'|同样在下一次,在问题本身中发布相关代码。您仍然可以发布JSFiddle链接,但仅作为附加内容。 – yuriy636

回答

0

的 “createDeleteButton” 是一个功能,但你不叫它。只是更改为:

shoppingListItem.appendChild(this.createDeleteButton()); 

,它应该工作