[英]How to call a function after mouseup (mouse release) in JavaScript?
I am trying to call a function after I release the mouse button, the function I am trying to call it after mouseup event is going to update the localstorage.
我在释放鼠标按钮后尝试调用 function,在 mouseup 事件更新本地存储后我尝试调用 function。 The problem is I can see the console.log but it doesn't make any changes on locolstorage.
问题是我可以看到 console.log 但它没有对 locolstorage 进行任何更改。
HTML HTML
<li class="item ui-state-default " draggable="true" id="${id}" onmouseup="mouseUp()">
<p class="text" >${tileName}</p>
<span class="close" tile="delete" id="${id}">×</span>
</li>
JavaScript JavaScript
function saveOrder() {
const char = document.querySelectorAll('.text');
const num = document.querySelectorAll('li');
const charArray = jQuery.makeArray(char);
const numArray = jQuery.makeArray(num);
nameArray = charArray.map(e => e.innerText);
idArray = numArray.map(e => e.id)
let saveIt = nameArray.map((i, idx) => ({
name: i,
id: +idArray[idx],
trash: false
}));
localStorage.setItem('TILES', JSON.stringify(saveIt));
}
function mouseUp() {
saveOrder()
console.log("mouseUP function called!")
}
The problem must be somewhere else in your code.问题一定出在您的代码中的其他地方。 I ran this in jsFiddle and it works.
我在 jsFiddle 中运行它并且它有效。 jsFiddle
jsFiddle
function mouseUp() {
function saveOrder() {
const char = document.querySelectorAll('.text');
const num = document.querySelectorAll('li');
const charArray = jQuery.makeArray(char);
const numArray = jQuery.makeArray(num);
nameArray = charArray.map(e => e.innerText);
idArray = numArray.map(e => e.id)
let saveIt = nameArray.map((i, idx) => ({
name: i,
id: +idArray[idx],
trash: false
}));
localStorage.setItem('TILES', JSON.stringify(saveIt));
}
saveOrder()
console.log("mouseUP function called!")
// Added lines
const tiles = JSON.parse(localStorage.getItem('TILES'))
console.log({tiles}) // console.log shows saved TILES
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.