我使用现在经典的".on"jquery处理程序来检查我添加了append的某个锚点上的点击。
$('#selector-to-thing-that-exists').on('click', '.thing-that-will-be-added-later', function() {
//I want to get the "data-pointnumber" and "data-goodanswer" on the clicked element.
});
这里的问题是$(this)给了我$("存在事物的#选择器"),而不是点击的元素(它是一个链接)。我够清楚吗?
$.each(this.currentQuestion.Reponse,function (arrayID,rep) {
$('#QuestionContainerSingle').find('.liste-boutons').append('<li><a href="" class="button-big btn-rep" data-nbpoints="'+rep.NbPoints+'" data-bonne="'+rep.Bonne+'">'+rep.Texte+'</a></li>');
});
$('#QuestionContainerSingle').on('click','.btn-rep',function (e) {
e.preventDefault();
console.log($(this).attr('data-nbpoints'));
});
这是html。。。由于它是一个更大的应用程序的一部分,所以提取这个问题所需的内容并不容易!
<div class="question-content" id="QuestionContainerSingle">
<div class="header-question">
<p class="intitule">
</p>
</div>
<div class="content">
<div class="title-box">Find the best answer</div>
<ul class="liste-boutons">
</ul>
</div>
</div>
终于找到了罪魁祸首!我使用this.btnRep=$('.btnRep');在我的应用程序的init中。当我使用时
$('#QuestionContainerSingle').on('click',**this.btnRep**,function (e) {
e.preventDefault();
console.log($(this).attr('data-nbpoints'));
});
它不起作用。但是当我使用时
$('#QuestionContainerSingle').on('click',**'.btn-rep'**,function (e)
它正在工作。。。站立!