我想知道在下面的代码中为每个元素调用函数的最佳方法是什么:
for (var y=1; y <= 24; y++) {
$("#christmasCalendar").append("<div class='span2'><h1><a href='#' data-toggle='tooltip' class='thumbnail' title='Click to add a question for this day'>"+y+"</a></h1></div>")
}
就像我想要调用这个函数一样:
$("#createChristmasDayQuiz").click(function () {
$("#QuizGuideInfo").delay(1000).fadeIn('slow');
$("div.infoHolder").html('<i class="fa-icon-bullhorn"></i>Select a color theme for your christmas calendar. This can be changed later.').hide().fadeIn('slow');
$('#createQuiz').modal('show');
});
我知道他们不能拥有相同的ID,但仍然是最好的解决方案吗?
提前致谢!
答案 0 :(得分:0)
您可以使用jQuery动态创建实际的DOM元素,而不是仅使用字符串插入它们。然后,您可以使用它们执行任何操作,包括将绑定功能绑定到其单击处理程序。
E.g。
var myNewElement = $('<div/>').addClass('div-class').appendTo('#christmasCalendar').click(function(e) {
alert('clicked on new element');
});
答案 1 :(得分:0)
您可以将“on”事件绑定到#christmasCalendar对象
$("#christmasCalendar").on("click", "a", function(e){
// handle link click event.
});
适用于所有链接