我有几个像这样设置的div:
<div class="answers_total">
<div data-answer="true" class="answer">SPEECH</div>
<div data-answer="false" class="answer">RELIGION AND BELIEF</div>
<div data-answer="true" class="answer">PRESS</div>
<div data-answer="true" class="answer">ASSEMBLY</div>
<div data-answer="false" class="answer">PETITION</div>
</div>
<div class="answers_total">
<div data-answer="false" class="answer">SPEECH</div>
<div data-answer="true" class="answer">RELIGION AND BELIEF</div>
<div data-answer="false" class="answer">PRESS</div>
<div data-answer="true" class="answer">ASSEMBLY</div>
<div data-answer="false" class="answer">PETITION</div>
</div>
我正在用 jQuery.map() 像这样添加真正的答案:
var x = $('.answers_total').map(function(i,v){
return $('div[data-answer=true]',v).length; // return how many have data-answer=true
});
我要做的是为点击编写一个超级简洁的函数。如果我要为每一个写出来,我会这样做:
var x = $(".answers_total").map(function(i,v) {
return $('div[data-answer=true]',v).length;
});
clicks = 0;
$(".answer").click(function() {
jthis = this;
if ( $(jthis).att("data-attribute") == "true" ) {
clicks++;
if ( clicks == x[0] ) {
alert('you found all the true answers');
}
}
});
像这样写的问题是我必须为每个“answers_total”制作一个版本。你将如何改变它,所以你只需要写一次?
感谢您在这方面提供的任何帮助。