我正在学习如何通过购物清单运动管理应用程序的状态。按照指示,我在那里我存储我添加任何物品的对象内部的数组:如何从阵列是一个对象的内部取出的特定元素?
var state = {
items: []
};
要修改state
我用这个函数:
var addItem = function(state, item) {
state.items.push(item);
};
它通过一个事件监听器后来被称为(并通过renderList
添加到DOM,这里没有显示):
$('#js-shopping-list-form').submit(function(event){
event.preventDefault();
addItem(state, $('#shopping-list-entry').val());
renderList(state, $('.shopping-list'));
});
如何从数组中删除我的state
对象内的具体项目?基本上我想要扭转,当用户点击<button class="shopping-item-delete">
上面的序列。
下面是最终解决方案的演示:https://thinkful-ed.github.io/shopping-list-solution/
HTML
<body>
<div class="container">
<form id="js-shopping-list-form">
<label for="shopping-list-entry">Add an item</label>
<input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
<button type="submit">Add item</button>
</form>
<ul class="shopping-list">
<li>
<span class="shopping-item">apples</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
</ul>
</div>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script type="text/javascript" src="app.js"></script>
</body>
当然,我们可以放手IE 7和8吧?这几乎是2017年!哈哈,反正填充工具都可以在这里https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill –
@Andy发现,这个伟大的工程!谢谢。 –