본문 바로가기

카테고리 없음

[jQuery] 동적 생성된 태그에 이벤트 주기

반응형

참고 : http://roqkffhwk.tistory.com/45


 dynamic_code-1.html

<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>동적 태그 생성</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {

    jQuery("#btn").click(function() {

        alert("동적으로 버튼을 생성하지 못합니다.");
        var dynamicTag = "<input type='button' id='btn' value='생성된 버튼'/><br/>";
        jQuery("#displayDiv").append(dynamicTag);
    });
});
</script>
</head>
<body>
    <h1>동적 태그 생성</h1>
    <hr/>
    <button type="button" id="btn">버튼 생성</button>
    <div id="displayDiv">
    </div>   
</body>
</html>

 



 dynamic_code-2

<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>동적 태그 생성</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {

    jQuery("#btn").on("click", function() {

        alert("동적으로 버튼을 생성합니다.");
        var dynamicTag = "<input type='button' id='btn' value='생성된 버튼'/><br/>";
        jQuery("#displayDiv").append(dynamicTag);
    });
});
</script>
</head>
<body>
    <h1>동적 태그 생성</h1>
    <hr/>
    <button type="button" id="btn">버튼 생성</button>
    <div id="displayDiv">
    </div>   
</body>
</html>

 



jQuery 버전 문제인지.

위의 버전이 작동을 한다.

확인 필요

반응형