๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[Search] ๊ฒ€์ƒ‰ ์œ ํ˜• ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ02

by _ํ† ๋งคํ†  2022. 8. 17.
728x90

includes()๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ ์ œ์ž‘ํ•˜๊ธฐ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ includes() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋ฅผ ์ œ์ž‘ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
๋จผ์ € includes() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์งง๊ฒŒ ์•Œ์•„๋ณด๊ณ  ์‚ฌ์ดํŠธ ์‹ค์Šต์— ๋“ค์–ด๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!


#1. includes()

includes() ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉฐ ๋ฌธ์ž์—ด ๋‚ด์—์„œ ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์˜ ํฌํ•จ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋ถˆ๋ฆฐ(true, false)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ์˜ˆ์ œ ์ฝ”๋“œ

const str1 = "javascript reference";

const currentStr1 = str1.includes("javascript");      // true
const currentStr2= str1.includes("j");                // true
const currentStr3= str1.includes("b");                // false : b๋Š” ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ false
const currentStr4= str1.includes("reference");        // true
const currentStr5 = str1.includes("reference", 1);    // true
const currentStr6 = str1.includes("reference", 11);   // true
const currentStr7 = str1.includes("reference", 12);   // false

#2. HTML : ๊ตฌ์กฐ ์งœ๊ธฐ

! point !
list ํƒœ๊ทธ์™€ data-name / data-type ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ด๋ฆ„๊ณผ ํƒ€์ž…์„ ์ •ํ•ด๋ณด์ž!

โœ๏ธ์ฝ”๋“œ ๋ณด๊ธฐ

<div class="search__wrap">
    <span>indexOf()๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ํ•˜๊ธฐ</span>
    <h1>HTML ํƒœ๊ทธ ๊ฒ€์ƒ‰ํ•˜๊ธฐ</h1>
    <div class="search__box">
        <label for="search">๊ฒ€์ƒ‰ํ•˜๊ธฐ</label>
        <input type="text" id="search" placeholder="HTML ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!">
    </div>
    <div class="search__list">
        <div class="html">
            <ul>
                <li data-name="accent-color" data-type="์š”์†Œ"><strong>accent-color</strong> : accent-color๋Š” ์š”์†Œ์˜ ๊ฐ•์กฐ ์ƒ‰์ƒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="align-content" data-type="ํ”Œ๋ ‰์Šค"><strong>align-content</strong> : align-content๋Š” ์ฝ˜ํ…์ธ  ์‚ฌ์ด์™€ ์ฝ˜ํ…์ธ  ์ฃผ์œ„ ๋นˆ ๊ณต๊ฐ„์„ ํ”Œ๋ ‰์Šค ๋ฐ•์Šค'์˜ ๊ต์ฐจ์ถ• ๋˜๋Š” ๊ทธ๋ฆฌ๋“œ์˜ ๋ธ”๋ก ์ถ•์„ ๋”ฐ๋ผ ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐฉ์‹์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="align-items" data-type="ํ”Œ๋ ‰์Šค"><strong>align-items</strong> : align-item์€ ์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ๋‚ด๋ถ€ ์ƒํ•˜๊ด€๊ณ„ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="align-self" data-type="ํ”Œ๋ ‰์Šค"><strong>align-self</strong> : align-self๋Š” ๊ฐœ๋ณ„์  ์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="allr" data-type="๊ธฐํƒ€"><strong>all</strong> : all์€ ์š”์†Œ์˜ ๋ชจ๋“  ์†์„ฑ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ์ƒ์†์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation</strong> : animation์€ ์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ์„ ์ผ๊ด„์ ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-delay" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-delay</strong> : animation-delay๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-direction" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-direction</strong> : animation-direction๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-duration" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-duration</strong> : animation-duration๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-fill-mode" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-fill-mode</strong> : animation-fill-mode๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋๋‚œ ํ›„์˜ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-iteration-count" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"> data-type="animation"<strong>animation-iteration-count</strong> : animation-iteration-count๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-name" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-name</strong> : animation-name์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ Keyframe ์ด๋ฆ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-play-state" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-play-state</strong> : animation-play-state๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์ง„ํ–‰ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-timeline" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-timeline</strong> : animation-timeline์€ ์š”์†Œ์— ์ ์šฉํ•  ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์„ค๋ช…ํ•˜๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ @scroll-timeline at-rules ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="animation-timing-function" data-type="์• ๋‹ˆ๋ฉ”์ด์…˜"><strong>animation-timing-function</strong> : animation-timing-function์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„์˜ ์†๋„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
                <li data-name="appearance" data-type="๊ธฐํƒ€"><strong>appearance</strong> : appearance๋Š” webit ๊ณ„์—ด์˜ ๋ธŒ๋ผ์šฐ์ €์˜ type="search" ํ•„๋“œ์˜ ๋‘ฅ๊ทผ ํ…Œ๋‘๋ฆฌ ๊ฐ’์ด๋‚˜ reset ํšจ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฒ„ํŠผ์„ ์‚ญ์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</li>
                <li data-name="aspect-ratio" data-type="์š”์†Œ"><strong>aspect-ratio</strong> : aspect-ratio๋Š” ์š”์†Œ์˜ ํฌ๊ธฐ๋ฅผ ๋น„์œจ๋Œ€๋กœ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.</li>
            </ul>
        </div>
    </div>
</div>

#3. CSS : ์Šคํƒ€์ผ์ฃผ๊ธฐ

! point !
๋งˆํฌ์—…์„ ํ† ๋Œ€๋กœ ์Šคํƒ€์ผ ์†์„ฑ์„ ์„ค์ •ํ•ด๋ณด์ž!
- ์Šคํƒ€์ผ ์†์„ฑ์€ ์ •ํ•ด์ง„ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ˆ ์‹ค์Šตํ•˜์‹ค ๋ถ„๋“ค์€ ์ง์ ‘ ์†์„ฑ์„ ์•Œ์•„๋ณด๊ณ  ์Šคํƒ€์ผ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค!

โœ๏ธ์†์„ฑ ๋ณด๊ธฐ

/* font color */
:root {
    --htmlColor : #223547;
    --cssColor : #472237;
    --javascriptColor : #624313;
}

/* reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'NexonLv1Gothic';
    color: var(--htmlColor);
}
*, *:before, *:after {
    box-sizing:border-box;
}
a {
    color: var(--htmlColor);
    text-decoration: none;
}
li {
    list-style: none;
}

/* header */
#header nav {
    margin: 10px;
}
#header nav li {
    display: inline;
    position: relative;
}
#header nav li a {
    width: 30px;
    height: 30px;
    border: 1px solid var(--htmlColor);
    border-radius: 50%;
    display: inline-block;
    text-align: center;
    line-height: 30px;
    font-family: 'NexonLv1Gothic';
}
#header nav li.active a {
    background-color: var(--htmlColor);
    color: #fff;
}
#header nav li .sub {
    position: absolute;
    left: 0;
    top: 35px;
    width: 400px;
}
#header nav li .sub li a {
    width: auto;   
    background-color: transparent;
    color: var(--htmlColor);
    border: 0;
    text-align: left;
    line-height: 1.2;
}
#header nav li .sub li.active a {
    text-decoration: underline;
}


/* main */
#main {
    margin: 50px 10px;
}
.search__wrap {
    max-width: 1400px;
    margin: 0 auto;
    border: 3px solid var(--htmlColor);
    border-radius: 20px;
    background-color: #F1F3F6;
    padding: 30px;
    text-align: center;
}
.search__wrap > span {
    font-size: 20px;
    margin-bottom: 20px;
    display: inline-block;
}
.search__wrap > h1 {
    font-family: 'Tmon';
    color: var(--htmlColor);
    font-size: 6vw;
    margin-bottom: 10px;
}
.search__box {
    margin-bottom: 40px;
}
/* irํšจ๊ณผ */
.search__box label {
    position: absolute;
    clip: rect(0 0 0 0);
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
}
.search__box input {
    border: 2px solid var(--htmlColor);
    padding: 15px 40px;
    width: 70%;
    border-radius: 50px;
    font-size: 20px;
}
.search__list li {
    text-align: left;
    line-height: 1.7;
}
.search__list li.hide {
    display: none;
}
.search__list li.show {
    display: block;
}
.search__info {
    text-align: right;
    margin-bottom: 30px;
    padding-bottom: 10px;
    border-bottom: 2px dashed var(--htmlColor);
}
.search__info .type {
    text-align: center;
    margin-bottom: 10px;
}

/* ๋ชจ๋ฐ”์ผ */
@media (max-width: 600px){
    .search__wrap {
        padding: 20px;
    }
    .search__wrap > span {
        font-size: 16px;
        margin-bottom: 10px;
    }
    .search__wrap > h1 {
        font-size: 44px;
    }
    .search__box input {
        font-size: 16px;
        padding: 12px 30px;
    }
}

/* footer */
#footer {
    text-align: center;
}
#footer a {
    color: #000;
    font-family: 'NexonLv1Gothic';
    padding-bottom: 50px;
}
#footer a:hover {
    text-decoration: underline;
}

#4. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ ์งœ๊ธฐ

includes()์™€ ๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„์„ฑํ•ด๋ณด์ž!
์ฝ”๋“œ์˜ ์ฃผ์„๊ณผ ํ•จ๊ป˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ดํ•ดํ•ด๋ณด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค:)

โœ๏ธ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ

//์„ ํƒ์ž
const searchBox = document.querySelector(".search__box input");       //๊ฒ€์ƒ‰ ์˜์—ญ
const searchList = document.querySelectorAll(".search__list ul li");  //๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");      //๊ฐฏ์ˆ˜

//์ „์ฒด ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length;

//searchBox ๊ฒ€์ƒ‰ ์˜์—ญ
searchBox.addEventListener("keyup",()=>{
    const searchWord = searchBox.value;     // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ‚ค์›Œ๋“œ

    searchList.forEach(el => {
        const cssName = el.dataset.name; //css์†์„ฑ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ„ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ด = CSS ์†์„ฑ์˜ ๋ชจ๋“  ๊ฐฏ์ˆ˜๊ฐ’ ์ถœ๋ ฅ
        const cssType = el.dataset.type; //CSS ์†์„ฑ์˜ ํƒ€์ž…๋ณ„ ๊ฐฏ์ˆ˜๊ฐ’์„ ๊ฐ€์ ธ์˜ด

        if(cssName.includes(searchWord) | cssType.includes(searchWord)){  //์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด true์ผ ๋•Œ show false๋ฉด ์ˆจ๊ธฐ๊ธฐ
            el.classList.remove("hide");
        } else {
            el.classList.add("hide");
        }
    });
});

! ๊ฒ€์ƒ‰ ์˜์—ญ์ด ์ž‘๋™๋˜๋Š” ๊ณผ์ • ์•Œ์•„๋ณด๊ธฐ !
***์ƒ์ˆ˜๋ช….includes(๊ฒ€์ƒ‰๊ฐ’) ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•œ ๋ฌธ์ž์—ด์ด ์ƒ์ˆ˜์— ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ ๋‚ด์— ํฌํ•จ๋˜์—ˆ์„ ๋•Œ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.***
1. ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ†ตํ•ด keyup(= ํ•œ ๊ธ€์ž๋ผ๋„ ์ž…๋ ฅํ•˜๋ฉด ์‹คํ–‰)์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
2. classList๋ฅผ ํ†ตํ•ด hide๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ false๊ฐ€ ๋ฐ˜ํ™˜๋˜์–ด ์†์„ฑ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆจ๊น๋‹ˆ๋‹ค.(โ†’ css ์†์„ฑ๊ฐ’์œผ๋กœ hide์—๊ฒŒ display: none;์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ!)
3. ๊ฐ’์ด true์ผ ๊ฒฝ์šฐ remove๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ถ”๊ฐ€ํ–ˆ๋˜ hide๋ฅผ ์ง€์šฐ๊ณ  ์†์„ฑ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์ด๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ ์ซŒ์ซŒ๋”ฐ๋ฆฌ ๋ฏธ์…˜ : for()๋ฌธ๊ณผ forEach()๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๋Š” ์‹ ๋งŒ๋“ค๊ธฐ!

๊ทธ๋™์•ˆ ์ž์ฃผ ๋‚˜์™”๋˜ ๋ฐ˜๋ณต๋ฌธ๋“ค์„ ์ด์šฉํ•ด์„œ ์ „์ฒด ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์‹๋„ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค!

for(let i=0; i<=searchList.length; i++){
    document.querySelector(".search__info .num").innerText = i;
}
searchList.forEach((el, i) => {
        document.querySelector(".search__info .num").innerText = i + 1;
});

#4. ๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css