๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Lect & Tip/javascript, jQuery

jQuery ๋™์  click ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ

by ๋‚ฏ์„ ๊ณต๊ฐ„2019 2023. 8. 23.

๋ชฉ์ฐจ

    jQuery๋กœ ๋™์  click ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ

    ๊ฐœ์š”

    ์›น ๊ฐœ๋ฐœ์—์„œ jQuery๋Š” ๋งŽ์€ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ๊ทธ ์ค‘ ํ•˜๋‚˜๋Š” ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์š”์†Œ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    ์ƒํ™ฉ ์„ค์ •

    ๋ณดํ†ต ์›น ํŽ˜์ด์ง€์—์„œ ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋ถ€์—ฌํ•  ๋•Œ, $(document).ready() ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ด๋ฒคํŠธ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฒ„ํŠผ์˜ ๊ฒฝ์šฐ, ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ๋Š” ํ•ด๋‹น ๋ฒ„ํŠผ์ด ์กด์žฌํ•˜์ง€ ์•Š์•„ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
    <title>Document</title>
    <script>
    $(document).ready(function() {
      // ์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
      $("#firstButton").on("click", function() {
        var bodyHtml = "<button id='secondButton'>๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ</button>";
        $("#mainDiv").append(bodyHtml);
      });
    
      // ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
      $("#secondButton").on("click", function() {
        alert("๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค.");
      });
    });
    </script>
    </head>
    <body>
    <div id="mainDiv">
      <button id="firstButton">์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ</button>
    </div>
    </body>
    </html>

    ์œ„ ์ฝ”๋“œ์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ (secondButton)์„ ํด๋ฆญํ•ด๋„ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ์˜ ์ด๋ฒคํŠธ๊ฐ€ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ ์ด๋ฏธ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

    ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

    ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์š”์†Œ์— ์ด๋ฒคํŠธ๋ฅผ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด์„  ๋‹ค๋ฅธ ๋ฐฉ์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์‹ , $(document).on("click", selector, handler) ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์šฐ์•„ํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
    <title>Document</title>
    <script>
    $(document).ready(function() {
      // ์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
      $("#firstButton").on("click", function() {
        var bodyHtml = "<button id='secondButton'>๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ</button>";
        $("#mainDiv").append(bodyHtml);
      });
    
      // ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
      $(document).on("click", "#secondButton", function() {
        alert("๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค.");
      });
    });
    </script>
    </head>
    <body>
    <div id="mainDiv">
      <button id="firstButton">์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ</button>
    </div>
    </body>
    </html>

    ๊ฒฐ๋ก 

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์š”์†Œ์—๋„ ์ ์ ˆํ•œ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $(document).on("click", selector, handler)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋™์  ์š”์†Œ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์— ์šฉ์ดํ•˜๋ฉฐ, ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ ์„ ์–ธ๋˜์ง€ ์•Š์€ ์š”์†Œ์—๋„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€