๐Ÿ‘ป [WHYGRAM] |[JS] | ๐ŸŽฎUNDO ๊ธฐ๋Šฅ , push() , pop()

0

๐Ÿ‘ป WHYGRAMย 

๋ชฉ๋ก ๋ณด๊ธฐ
10/16
post-thumbnail

๐ŸŸฆ 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();
        }
    }
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

0๊ฐœ์˜ ๋Œ“๊ธ€