다음 코드에서 jQuery를 사용하여 div에 표시된 배열에 몇 가지 주석이 있습니다. 각 댓글에는 새로운 댓글을 게시 할 때까지 제대로 작동하는 옵션 버튼이 있습니다. 각 요소에 대해 고유 ID를 사용했지만 사용하지 못했습니다.동적으로 생성 된 html 요소가 작동을 멈춤
페이지가로드되면 옵션 버튼이 작동합니다. 하지만 새로운 설명을 제출하면 버튼이 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
여기 내 스크립트입니다 :
var i = 0;
var comments_display= "";
var comments = ['Hello World!', 'Hello! This is a comment.'];
//reads the entire array, creates the content, and sends it to the div
function show_comments(){
for (i=0; i<comments.length; i++){
comments_display += "<div class='single_comment_container'>";
comments_display += "<div class='comment_comment'>" + comments[i] + "</div>";
comments_display += "<div class='options'>Options</div></div>";
}
$("#comment_container").html(comments_display);
comments_display = "";
}
//appends a new comment to the array
function new_comment(){
if ($("#comment_input").val() == null || $("#comment_input").val() == ""){
alert("Your comment must be at least 1 character long.");
}
else{
comments.push($('#comment_input').val());
show_comments();
$("#comment_input").val("");
}
}
$(document).ready(function(){
show_comments();
$("#submit_comment").click(function(){
new_comment();
});
//display a message when an element of the class 'options' is clicked
$(".options").click(function(){
alert("OPTIONS");
});
});
그리고 여기에 어떻게 작동하는지 확인하기 위해 바이올린입니다. http://jsfiddle.net/fahKb/3/
시간을내어이 질문을 읽어 주셔서 감사합니다.
$(document).on('click', '.options', function() {
alert("OPTIONS");
});
참고 :
좋은 정적 부모 요소는'# comment_container'입니다. –
그랬습니다! 대단히 감사합니다. 저는 대표단에 대해 몰랐습니다. – user1899500