ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

JS

Context Menu

choi95 2021. 6. 1. 18:48

Question

๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ณ , ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๊ทธ ์™ธ์˜ ๋ถ€๋ถ„์„ ํด๋ฆญํ•˜๋ฉด ์‚ฌ๋ผ์ง€๋Š” ํŒ ์˜ค๋ฒ„ ์ปดํฌ๋„ŒํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ

 

Solution

const wrapper = document.querySelector('.wrapper');
const items = document.querySelectorAll('.item');
const contexts = document.querySelectorAll('.context');

function hide(e) {
    for(let x of contexts) {
        if(x.style.display === 'block') {
            x.style.display = 'none';
            x.parentNode.classList.remove('open');
        }
    }
}
function popUp(e) {
    event.stopPropagation();
    for(let x of contexts) {
        if(x.style.display === 'block') {
            x.style.display = 'none';
            x.parentNode.classList.remove('open');
        }
    }
    e.target.classList.add('open');
    const context = e.target.firstElementChild;
    context.style.display = 'block';

}

wrapper.addEventListener('click', hide);
items.forEach(item => {item.addEventListener('click', popUp)});


 

Issue

   1. querySelectorAll์€ DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด์ธ NodeList๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. NodeList๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๊ธฐ ๋•Œ๋ฌธ์—                       Array.prototype.method()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

   2. HTML ์š”์†Œ ์‚ฌ์ด์˜ ์ŠคํŽ˜์ด์Šค, ํƒญ, ์ค„๋ฐ”๊ฟˆ(๊ฐœํ–‰) ๋“ฑ์˜ ๊ณต๋ฐฑ ๋ฌธ์ž๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž์‹

       ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด firstChild๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์— ์˜ค๋ฅ˜ ๋ฐ ์›์น˜ ์•Š๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

       ์ด๋ฅผ ๋Œ€์‹ ์— firstElementChild๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. firstElementChild๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•˜๊ณ  ์š”์†Œ ๋…ธ๋“œ๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

   3.  ์ž์‹ ์š”์†Œ์˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋Š” ์ดํ›„์— ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ชจ ์š”์†Œ์˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์— ์˜ํ•ด ๋ฐฉํ•ด๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ์ด๋ฒคํŠธ์˜ ๋ฒ„๋ธ”๋ง ๋•Œ๋ฌธ์— 

        ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ์œผ๋กœ ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์— stopPropagation()์„ ํ†ตํ•ด ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Refactoring

const wrapper = document.querySelector('.wrapper');
const items = document.querySelectorAll('.item');

wrapper.addEventListener('click', function(e) {
    const targetElem = e.target;
    event.stopPropagation();
    if(!targetElem.classList.contains('item')) return;
    targetElem.classList.toggle('open');
    items.forEach(function(elem) {
        if(elem !== targetElem) elem.classList.remove('open');
    })
})

document.body.addEventListener('click', function(e) {
    if(e.target.classList.contains('context')) return
    items.forEach(function(elem) {
        elem.classList.remove('open');
    })
})

 

Issue

   1. ๋ชจ๋“  ์š”์†Œ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๊ฒƒ์€ ๊ณผ๋ถ€ํ™” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ณ  ์ดํ›„ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ ๋  ์š”์†Œ์— ์ƒˆ๋กœ์šด ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ

      ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ ๋˜ํ•œ ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค. ์ด์— ์ƒ์œ„ ๋ถ€๋ชจ ์š”์†Œ ๋…ธ๋“œ์—๋งŒ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•ด ์ค€ ๋’ค ํ•˜์œ„ ์š”์†Œ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ๊ฐ์ง€๋Š”

      ์บก์ณ๋ง์„ ํ†ตํ•ด์„œ ์ด๋ฃจ์–ด ์ง€๋„๋ก ํ•œ๋‹ค. 

 

    2. classList.add()์™€ remove()๊ฐ€ ์•„๋‹Œ toggle() ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋งŒ์œผ๋กœ ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐ ๊ธฐ๋Šฅ ๊ตฌํ˜„

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ