0
我试图用html5 localstorage和一些javscript/jquery做一个todo-list。我使用一个ul并存储它,这工作得很好,并且能够将li添加到它,并且当我重新加载页面时它们保持不变。但是,当试图做一个删除功能,我遇到了一些问题.. 下面的代码工程删除李的后,我重新加载页面。但我不能删除刚刚添加的li。 (对不起,凌乱的英语......)只能在页面重新加载时删除
增加一个项目里我喜欢这样,当:
$(add).click(function(e) {
if (addtext.value != ""){
$(listan).append("<li>" + addtext.value + "</li>"); //listan is my <ul>
localStorage.setItem('todo', listan.innerHTML);
addtext.value = "";
color(); //this adds some color to the li and also adds a delete-button
}
}
颜色() - 功能:
function color(){
lin = document.getElementsByTagName('li');
for (var i = 0; i < lin.length;i++) {
if (!(lin[i].childNodes.length > 1)){
$(lin[i]).append("<input type='button' value='ta bort' class='tabort'></intput>"); //adds a deletebutton to all li's that doesnt have one
}}}
和取出物品时我这样做:
$('input[type="button"]').click(function() {
if (this.id != 'clear'){
$(this).parent().remove();
color();
localStorage.setItem('todo', listan.innerHTML);
}
});
任何想法?
活在1.7被弃用,使用。对代替 – Rodik
哦,谢谢@Rodik ,不知道 – CambridgeMike
真棒!谢谢!但它应该是直播(**'点击'**,功能(){在第一个权利? – tobbe