[英]i want to delete an item from local storage
actually i just want to grab the text inside the li elemets实际上我只想抓取 li elemets 中的文本
Here is the html这是html
<ul id="list">
<li class="list-item">dfsdf<button class="del-btn">delete</button></li>
<li class="list-item">dfsd<button class="del-btn">delete</button></li>
</ul>
so when i click on the first delete button i want to grab 'dfsdf' as string and when i click the the second delete button i want the value to be 'dfsd' as string因此,当我单击第一个删除按钮时,我想将 'dfsdf' 作为字符串抓取,而当我单击第二个删除按钮时,我希望该值为 'dfsd' 作为字符串
and e.target.parentElement.textContent gives dfsdfdelete and dfsddelete respectively when i click the first and the second button because of the button with text delete, i want e.target.parentElement.textContent = dfsdf and e.target.parentElement.textContent = dfsd respectively由于带有文本删除的按钮,当我单击第一个和第二个按钮时,e.target.parentElement.textContent 分别给出 dfsdfdelete 和 dfsddelete,我想要 e.target.parentElement.textContent = dfsdf 和 e.target.parentElement.textContent = dfsd 分别
to delete anything from localstorage add your key into next code.要从 localstorage 中删除任何内容,请将您的密钥添加到下一个代码中。
localStorage.removeItem('keyName');
or或者
delete window.localStorage["keyName"]
You might be looking for Text.prototype.wholeText
along with HTMLElement.prototype.closest
:您可能正在寻找Text.prototype.wholeText
和HTMLElement.prototype.closest
:
for (const delBtn of document.querySelectorAll('.del-btn')) { delBtn.addEventListener('click', function() { const liText = this.closest('li').childNodes[0].wholeText; // alternative: // const liText = this.previousSibling.textContent; // or // const liText = this.parentElement.childNodes[0].wholeText; // but those ones rely even more strictly on your current DOM structure console.log(liText); }) }
<ul id="list"> <li class="list-item">dfsdf<button class="del-btn">delete</button></li> <li class="list-item">dfsd<button class="del-btn">delete</button></li> </ul>
you can use :您可以使用 :
localStorage.removeItem(tasks);
Or或者
localStorage.clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.