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

[JavaScript / jQuery] Alert ๋ฐ•์Šค ์ฐฝ ์—ฌ๋‹ซ๊ธฐ ๋ฐ ์ฐฝ ๋‚ด์šฉ ๋ฐ”๊พธ๊ธฐ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

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

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

  • ์ฒซ ํ™”๋ฉด์—์„œ๋Š” ๋ฒ„ํŠผ 1๊ณผ ๋ฒ„ํŠผ 2๋งŒ ๋ณด์ด๊ณ , ๊ทธ ์ƒํƒœ์—์„œ ๋ฒ„ํŠผ 1์„ ๋ˆ„๋ฅด๋ฉด ๋‚ด์šฉ์ด "์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." ์ธ ์•Œ๋ฆผ์ฐฝ์„ ๋ณด์ด๊ฒŒ ํ•˜๊ณ , ๋ฒ„ํŠผ 2๋ฅผ ๋ˆ„๋ฅด๋ฉด "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." ๋ผ๋Š” ์•Œ๋ฆผ์ฐฝ์ด ๋ณด์ด๊ฒŒ ํ–ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์–ด๋Š ์ฐฝ์ด ๋– ๋„ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์•Œ๋ฆผ์ฐฝ์ด ์•ˆ ๋ณด์ด๊ฒŒ๋” ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿ’œ HTML ๊ณผ CSS

  • ์ผ๋‹จ ์ตœ์ข…ํ™”๋ฉด์ธ ์•Œ๋ฆผ์ฐฝ๊ณผ ๋ฒ„ํŠผ 1, ๋ฒ„ํŠผ 2๊ฐ€ ๋‚˜์˜ค๋„๋ก HTML, CSS ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.
  • ์šฐ์„  ๋‚ด์šฉ์€ "์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." ์™€ "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." ์ด ๋‘ ๊ฐ€์ง€๋กœ๋งŒ ์ „ํ™˜๋˜๋Š” ๊ฑฐ๋ผ ๊ณตํ†ต๋˜๋Š” ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  "์•„์ด๋””", "๋น„๋ฐ€๋ฒˆํ˜ธ" ๋ผ๋Š” ๋ฌธ๊ตฌ๋งŒ ๋ฐ”๋€” ์ˆ˜ ์žˆ๊ฒŒ ์ด ๋ถ€๋ถ„๋งŒ id ๋ฅผ title ๋กœ ๋ถ€์—ฌํ–ˆ๋‹ค.
  • ์ฒซ ํ™”๋ฉด์—์„œ๋Š” alert-box ๊ฐ€ ๋ณด์ด์ง€ ์•Š๋„๋ก display: none ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. ์•„๋ž˜ CSS ํŒŒ์ผ์—์„œ๋Š” id ๋ฅผ alert ๋กœ ๋ถ€์—ฌํ•ด์ฃผ๊ธฐ ์ „์ด๋ผ id ๊ฐ€ ์•„๋‹Œ class ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.
<div class="container">
    <div class="alert-box" id="alert">
        <h3>์•Œ๋ฆผ์ฐฝ์ž…๋‹ˆ๋‹ค.</h3>
        <p><span id="title"></span>๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.</p>
        <button id="close">๋‹ซ๊ธฐ</button>
        <div style="clear:both"></div>
    </div>
    <div class="btns">
        <button class="btn">๋ฒ„ํŠผ 1</button>
        <button class="btn">๋ฒ„ํŠผ 2</button>
    </div>
</div>

 

.container {
  padding: 30px;
  width: 100%;
}

button:hover {
  cursor: pointer;
}

.alert-box {
  margin: 10px auto;
  padding: 15px;
  border: 2px solid red;
  width: 400px;
  border-radius: 5px;
  display: none;
}

.alert-box h3 {
  text-align: center;
  margin-bottom: 20px;
}

.alert-box button {
  float: right;
  padding: 5px 10px;
  border: none;
  background: gold;
}

.btns {
  margin: auto;
  width: 400px;
  display: flex;
  justify-content: space-between;
}

.btn {
  width: 45%;
  padding: 5px 10px;
  border: none;
  background: gold;
}

๐Ÿ’œ JavaScript

  • showText ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ title ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜์—ฌ id ๊ฐ€ title ์ธ ํƒœ๊ทธ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋‚ด์šฉ์ด ๋‚˜์˜ค๊ณ , display : block ์ด ๋˜์–ด ์ฐฝ์ด ๋œจ๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.
  • ๋˜ํ•œ ์ด๋ฒˆ์—๋Š” addEventListener ๋ฅผ ์‚ฌ์šฉํ•ด์„œ id ๊ฐ€ close ์ธ ํƒœ๊ทธ๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น div ๋ฐ•์Šค๊ฐ€ ์•ˆ ๋ณด์ด๋„๋ก ์„ค์ •ํ–ˆ๋‹ค. 
function showText(title){
    document.getElementById('title').innerHTML=title;
    document.getElementById('alert').style.display='block';
}

document.getElementById('close').addEventListener('click', function(){
    document.getElementById('alert').style.display='none'
})
  • ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด button ํƒœ๊ทธ์˜ onclick ์†์„ฑ์— showText ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•Œ๋ฆผ์ฐฝ์ด ๋œจ๋ฉด์„œ ๋ฒ„ํŠผ 1๊ณผ ๋ฒ„ํŠผ 2๋ฅผ ๊ฐ๊ฐ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋œ๋‹ค.
<button onclick="showText('์•„์ด๋””')" class="btn">๋ฒ„ํŠผ 1</button>
<button onclick="showText('๋น„๋ฐ€๋ฒˆํ˜ธ')" class="btn">๋ฒ„ํŠผ 2</button>
728x90
๋ฐ˜์‘ํ˜•