UNDO
๊ธฐ๋ฅmemento
ํ์https://stackoverflow.com/questions/54416318/how-to-make-a-undo-redo-function
const mementos = []
const input = document.querySelector('input')
function saveMemento() {
mementos.push(input.value)
}
function undo() {
const lastMemento = mementos.pop()
input.value = lastMemento ? lastMemento : input.value
}
<h4> Type some characters and hit Undo </h4>
<input onkeydown="saveMemento()" value="Hello World"/>
<button onclick="undo()">Undo</button>
js
๋ฐฐ์ด์ push()
, pop()
ํจ์
push
: ๋ฐฐ์ด์ ๋์ ์์ดํ ์ ์ถ๊ฐํ๋ค.pop
: ๋ฐฐ์ด์ ๋ง์ง๋ง ์์ดํ ์ ์ ๊ฑฐํ๋ค.
elem.classList.add()
elem.append() + elem.lastElementChild.remove()
addEvent : function (elem) {
const imgElem = elem.firstElementChild;
elem.addEventListener('mouseover', () => {
imgElem.classList.add('blurImg');
this.showIcn(elem);
});
elem.addEventListener('mouseout', () => {
imgElem.classList.remove('blurImg');
this.hideIcn(elem);
});
},
showIcn : function (elem) {
const favIcn = this.favIcn(elem.dataset.isfav);
const cmtIcn = this.cmtIcn(elem.dataset.iscmt);
const icnElem = document.createElement('div');
icnElem.id = 'icn';
icnElem.append(favIcn);
icnElem.append(cmtIcn);
icnElem.style.position = 'absolute';
elem.append(icnElem);
},
hideIcn : function (elem) {
if(elem.childElementCount>1) {
elem.lastElementChild.remove();
}
}