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

 


https://choi95.tistory.com/86

 

auto-complete_๋น„๋™๊ธฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜, focusEvent, mouseEvent

๋‹ค์Œ๊ณผ ๊ฐ™์ด Input ํƒœ๊ทธ์— ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ด€๋ จ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด์„œ ์ถ”์ฒœ ๊ฒ€์ƒ‰์–ด๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๋„๋ก ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค. debounce๋ฅผ ํ†ตํ•œ request ํ˜ธ์ถœ ์ตœ์†Œํ™” ์‚ฌ์šฉ์ž Input ํƒœ๊ทธ์— ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅ

choi95.tistory.com

์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๊ตฌํ˜„ ์ž๋™ ์ถ”์ฒœ ๊ฒ€์ƒ‰ ๋ฆฌ์ŠคํŠธ์— ์ถ”์ฒœ์œผ๋กœ ๋œฌ ์™„์„ฑ๋œ ํ‚ค์›Œ๋“œ๋ฅผ ํด๋ฆญํ•˜๋ฉด Input ํƒœ๊ทธ value์— ๋ฐ”์ธ๋”ฉ๋˜๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

this.searchListElement.addEventListener('click', e => {
            let targetItem = e.target.textContent;
            this.textinputElement.value = '';
            this.textinputElement.value = targetItem; 
            this.hiddenElem(this.searchListElement);
        })

ํ•˜์ง€๋งŒ ์œ„ ์˜์ƒ๊ณผ ๊ฐ™์ด ํด๋ฆญํ•œ ์œ„์น˜์— ๋”ฐ๋ผ Input ํƒœ๊ทธ์— ํ‚ค์›Œ๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

innerText๋ฅผ ํ†ตํ•œ ํ•ด๊ฒฐ

๋ฆฌ์ŠคํŠธ์—์„œ ํ‚ค์›Œ๋“œ ์ž์ฒด๊ฐ€ ์•„๋‹Œ ํ•ญ๋ชฉ ๋ถ€๋ถ„์„ ํด๋ฆญํ•  ๊ฒฝ์šฐ์—๋Š”

ํ‚ค์›Œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š” span ํƒœ๊ทธ๋ฅผ ๊ฐ์‹ธ๋Š” wrapper์— ์ด๋ฒคํŠธ ํƒ€๊ฒŸ์ด ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

 

textContent๋Š” ์ŠคํŽ˜์ด์Šค(๊ณต๋ฐฑ)์„ ํฌํ•จํ•œ ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ๋‹ค. ๋ฐ˜๋ฉด์— innerText๋Š” ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ  ํ…์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— wrapper ํƒœ๊ทธ์™€ span ํƒœ๊ทธ ์‚ฌ์ด์— ์กด์žฌํ–ˆ๋˜ ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ  ๋ฌธ์ž๋งŒ ๊ทธ๋ž˜๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๋‹ค๋งŒ, ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ •ํ™•ํ•œ ์ด๋ฒคํŠธ ํƒ€๊ฒŸํŒ…์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์œ„ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ด๋ฒคํŠธ ์œ„์ž„์„ ํ•  ๊ฒฝ์šฐ์—๋Š” ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€๊ฒŸํŒ…ํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š”๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ผ ๊ฒƒ ๊ฐ™๋‹ค.

 

๋น„๋™๊ธฐ ํ›„์† ์ฒ˜๋ฆฌ์—์„œ ํ•ด๊ฒฐ

์™ธ๋ถ€์— ๋ณ„๋„๋กœ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด Input ํƒœ๊ทธ์— value๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด์ „ ํฌ์ŠคํŒ…์—์„œ์˜ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ์—ˆ๋‹ค.

requestAdapter.get(this.requestUrl, targetText)
                .then((result) => {
                    this.displayWordList(
                        this.searchListElement,
                        result
                    ).forEach((element, index) => { // document.querySelectorAll('.auto-complete-item-box') ๋ฐ˜ํ™˜
                        element.addEventListener('click', () => {
                            this.textinputElement.value = result[index].text;
                            this.hiddenElement(this.searchListElement);
                        });
                    });
                });

 

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ