상세 컨텐츠

본문 제목

[j쿼리] document에서 button감지해서 미리 action취하는법

카테고리 없음

by esoesmio 2023. 5. 25. 20:16

본문

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>

    <style>

    </style>
</head>
<body>

<p id="txt"></p>

</body>

<script>
    let cnt = 0;

    // $("#btn").click(() => {
    //     $("#txt").text(`${++cnt}회 클릭`);
    // });

    $(document).on("click", "#btn", () => {
        $("#txt").text(`${++cnt}회 클릭`);
    });

    //원래 존재하지 않던 태그를 생성하는 것을 동적태그 생성이라고 하고 생성된 태그를 동적 태그라고 한다.
    let htmlStr = `
            <button type="button" id="btn">클릭</button>
        `;

    $("#txt").after(htmlStr);






//     let cnt = 0;
// //원래 존재하지 않던 태그를 생성하는 것을 동적태그 생성이라고 하고 생성된 태그를 동적 태그라고 한다.
// $(document).on('click',"#bnt",()=>{
//     $("#txt").text(`${++cnt}회 클릭`);
//
//
//
// });
//
//
// let htmlstr = `<button type="button" id = "btn">클릭</button>`;
// $("#txt").after(htmlstr);
//
// // $("#btn").click(()=>{
// //
// //     $("#txt").text(`${++cnt}회 클릭`);
// // })
//
// //이렇게 이벤트 등록하고 동적태그 쓰면 안되는데
//
//
// //
// // $("#btn").on('click',()=>{
// //
// //     $("#txt").text(`${++cnt}회 클릭`);
// // })
//
// let l = `<li>hahahaha</li>`
// $("#txt").append(l);







</script>
</html>

댓글 영역