๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/JavaScript jQuery

[JavaScript / jQuery] ํŒŒ๋ผ๋ฏธํ„ฐ ํ™œ์šฉํ•˜์—ฌ Alert ๋ฐ•์Šค ์ฐฝ ์—ฌ๋‹ซ๊ธฐ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

by Connecting-the-dots 2022. 3. 3.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • ์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•Œ๋ฆผ์ฐฝ ์„ธ ๊ฐœ๋ฅผ ๋„์šฐ๊ณ  ๊ฐ ์•Œ๋ฆผ์ฐฝ์˜ X ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ์•Œ๋ฆผ์ฐฝ์ด ์‚ฌ๋ผ์ง€๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

๐Ÿ’œ HTML ๊ณผ CSS

  • ์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ƒ๋‹จ์— ์•Œ๋ฆผ์ฐฝ๋“ค์ด ๋œจ๊ฒŒ ํ•˜๋ ค๋ฉด ์ผ๋‹จ html ๊ณผ CSS ๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
  • X ๋ชจ์–‘ ์•„์ด์ฝ˜์€ Font Awesome ์„ ํ™œ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.
<div id="alert">
    <div class="alert-box yellow" id="yellow">
        <span class="wow">Holy guacamole!</span>
        <span> You should check in on some of those fields below.</span> 
        <i class="fas fa-times close"></i>
    </div>
    <div style="clear:both;"></div>
    <div class="alert-box blue" id="blue">
        <span class="wow">Holy guacamole!</span>
        <span> You should check in on some of those fields below.</span> 
        <i class="fas fa-times close"></i>
    </div>
    <div style="clear:both;"></div>
    <div class="alert-box grey" id="grey">
        <span class="wow">Holy guacamole!</span>
        <span> You should check in on some of those fields below.</span> 
        <i class="fas fa-times close"></i>
    </div>
    <div style="clear:both;"></div>
</div>
<button>์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ</button>
  • id ๊ฐ€ alert ์ธ ํƒœ๊ทธ๋Š” display: none ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด ์ฒซ ํ™”๋ฉด์—์„œ๋Š” ์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ ๋ฒ„ํŠผ๋งŒ ๋‚˜์˜ค๋„๋ก ํ–ˆ๋‹ค.
#alert {
  display: none; /* ์•Œ๋ฆผ์ฐฝ ์ˆจ๊ธฐ๊ธฐ */
  margin: 20px;
}

.alert-box {
  border: 1px solid black;
  padding: 15px 10px 15px 20px;
  margin-top: 10px;
  width: 600px;
  border-radius: 5px;
  font-size: 16px;
}

.alert-box i {
  float: right;
  font-size: 20px;
  color: rgb(179, 179, 179);
  width: 30px;
}

.yellow {
  color: rgb(139, 97, 19);
  background: rgb(255, 250, 175);
  border: 1px solid rgb(255, 238, 0);
}

.blue {
  color: rgb(67, 113, 197);
  background: rgb(196, 216, 253);
  border: 1px solid rgb(131, 168, 238);
}

.grey {
  color: rgb(102, 102, 102);
  background: rgb(214, 214, 214);
  border: 1px solid rgb(173, 173, 173);
}

.wow {
  font-weight: bold;
}

.close:hover {
  cursor: pointer;
}

button {
  margin: 10px 20px;
  padding: 10px 15px;
  border: none;
  background: red;
  color: white;
  border-radius: 5px;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
}

๐Ÿ’œ JavaScript

  • ์ด์ œ ์ค‘์š”ํ•œ JavaScript ์ฐจ๋ก€์ธ๋ฐ.. ์‚ฌ์‹ค ํƒœ๊ทธ ์ž์ฒด์—๋‹ค onclick ์†์„ฑ์„ ๋„ฃ์–ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ํƒœ๊ทธ๊ฐ€ ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ script ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค๋“  js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด link ๋ฅผ ๊ฑฐ๋Š” ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ๊น”๋”ํ•˜๊ณ  ์ข‹๋‹ค.
// ์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ์•Œ๋ฆผ์ฐฝ ํƒœ๊ทธ ์ „์ฒด ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
<button onclick="document.getElementById('alert').style.display='block'">์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ</button>

// X ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ id ๊ฐ€ yellow ์ธ ํƒœ๊ทธ ์•ˆ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
<i class="fas fa-times close" onclick="document.getElementById('yellow').style.display='none'"></i>
  • ๋˜ํ•œ ๊ฝค๋‚˜ ๊ฐ™์€ ๋‚ด์šฉ๋“ค์ด ๋ฐ˜๋ณต๋˜๋ฏ€๋กœ ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹ถ์—ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด  id ์™€ display ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.
function alert(id, display){
    document.getElementById(id).style.display=display;
}
  • ๊ทธ๋ฆฌ๊ณ  ์ด๊ฑธ ํƒœ๊ทธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์šฉํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.
// ์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ์•Œ๋ฆผ์ฐฝ ํƒœ๊ทธ ์ „์ฒด ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
<button onclick="alert('alert','block')">์•Œ๋ฆผ์ฐฝ ๋„์šฐ๊ธฐ</button>

// X ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ id ๊ฐ€ yellow ์ธ ํƒœ๊ทธ ์•ˆ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
<i class="fas fa-times close" onclick="alert('yellow','none')"></i>
728x90
๋ฐ˜์‘ํ˜•