[英]Javascript event listener behavior
我更改了ul中的文本區域元素時,將偵聽器添加到無序列表以執行功能。 更改文本區域后,我想獲取現在更改的文本區域的值,並將其保存。 當我嘗試將值保存在newNotes中時,我得到的是文本區域的INITIAL值,而不是更改了文本區域后的值。 難道不是在更改時觸發偵聽器的全部功能嗎?
ul.addEventListener('change',(e)=> {
if(e.target.tagName === "TEXTAREA") { // if the ul was changed and a textarea was targeted
const li = e.target.parentNode; // the parent list item of the text area
const liName = li.firstChild.textContent; // this is a string
var newNotes = e.target.textContent; // PROBLEM : RETURNS WRONG VALUE
console.log(newNotes);
updateNotesTo(liName, newNotes); // regarding localStorage
}
});
您需要來自textarea的value
更改
var newNotes = e.target.textContent;
至
var newNotes = e.target.value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.