JS原生第三题(增改删过滤)
//removeLocalStorage(删除本地数据)
function removeTaskFromLocalStorage(taskItem){
//为字符串类型的数据
// let ex = taskItem.textContent;
// console.log( typeof ex);
//数据
let caches;
//getItem读取数据
if (localStorage.getItem('caches') == null) {
//讲json格式转换为数组,因为数组的方法比较多
caches = [];
} else {
// 得到的字符串类型的数据,需要转换成数组
caches = JSON.parse(localStorage.getItem('caches'));
}
//index肯定为-1,因为是数组遍历的,怎么样结果都为-1 //bug
let index = caches.indexOf(taskItem.textContent);
caches.splice(index,1);
localStorage.setItem('caches',JSON.stringify(caches));
}
第一种方法跟着B站视频敲的没有效果,(不知道为什么没效果)
第二种方法是用indexOf