选择页面上具有特定类的最后一个元素
Select last element on page with specific class
我正在进行一个quizz测试。我想用一个特定的类选择页面上的最后一个元素,这样当用户点击它时,就会出现quizz的结果。现在我通过添加一个额外的类来完成这项工作,但我想知道是否有一种方法可以在不使用类的情况下遍历文档,这样无论有多少问题被添加到quizz中,当点击最后一个元素时,函数都会被触发。我在这里和这里尝试了建议的解决方案,但似乎不起作用。感谢
我为它创建了以下代码笔
//get total of questions
var $questionNumber = $('h2').length;
console.log($questionNumber);
//caching final score
var $totalScore=0;
$('li').click(function(){
//caching variables
var $parent = $(this).parent();
var $span = $(this).find('.fa');
//deactivate options on click
$parent.find('li').off("click");
//check for .correct class
//if yes
if($(this).hasClass('correct')){
//add .correctAnswer class
$(this).addClass('correctAnswer');
//find next span and change icon
$span.removeClass('fa fa-square-o').addClass('fa fa-check-square-o');
//reduce opacity of siblings
$(this).siblings().addClass('fade');
//show answer
var $answerReveal= $parent.next('.answerReveal').show();
var $toShowCorrect = $answerReveal.find('.quizzAnswerC');
var $toShowFalse = $answerReveal.find('.quizzAnswerF');
$toShowCorrect.show();
$toShowFalse.remove();
//add 1 to total score
$totalScore+=1;
//console.log($totalScore);
}else{
//add .wrongAnswer class
$(this).addClass('wrongAnswer').addClass('fade');
//change icon
$span.removeClass('fa fa-square-o').addClass('fa fa-check-square-o');
//reduce opacity of its siblings
$(this).siblings().addClass('fade');
//show wrong Message
var $answerReveal= $parent.next('.answerReveal').show();
var $toShowCorrect = $answerReveal.find('.quizzAnswerC');
var $toShowFalse = $answerReveal.find('.quizzAnswerF');
$toShowCorrect.remove();
$toShowFalse.show();
//locate correct and highlight
$parent.find('.correct').addClass('correctAnswer');
};
});//end click function
//print Results
function printResult(){
var resultText = '<p>';
if ($totalScore === $questionNumber){
resultText+='You got '+ $totalScore+ ' out of '+$questionNumber+'! </p>';
$('.resultContainer').append(resultText);
$('#halfText').append('<p>This is awesome!</p>');
$('#halfImage').append('<img src="http://placehold.it/350x150" width="100%"><img>');
}else if($totalScore>=3 && $totalScore < $questionNumber){
resultText+='You got '+ $totalScore+ ' out of '+$questionNumber+'! </p>';
$('.resultContainer').append(resultText);
$('#halfText').append('<p>So and so...better luck next time</p>');
$('#halfImage').append('<img src="http://placehold.it/350x150" width="100%"><img>');
}else if ($totalScore<3){
resultText+='You got '+ $totalScore+ ' out of '+$questionNumber+' </p>';
$('.resultContainer').append(resultText);
$('#halfText').append('<p>No..no...no...you have to try harder</p>');
$('#halfImage').append('<img src="http://placehold.it/350x150" width="100%"><img>');
}
};//end function
//final score
$('li.last').click(function(){
//show result after last li is clicked
var $height = $('.finalResult').height();
printResult();
console.log($totalScore)
$('.finalResult').show();
$('html, body').animate({
scrollTop: $(document).height()-$height},
1400);
});
使用jQuery:很容易获得给定类的最后一个元素
$('selector').last().click(...);
您可以使用选择类的最后一个元素
$( ".classname:last-child" ).click(function(){});
相关文章:
- 表追加而不附加最后一个元素
- 如何查找流星集合中最后一个元素/对象的id
- 如何在使用Protractor测试时找到ng中继器的最后一个元素
- 在元素数组上循环只会影响最后一个元素
- 推特引导滚动间谍总是选择最后一个元素
- 选择页面上具有特定类的最后一个元素
- 检查 JQuery 每个函数中的最后一个元素
- 选择表中的最后一个元素
- forEach 仅执行数组的最后一个元素
- JavaScript,数组和函数 - 只有数组的最后一个元素有效
- jQuery - 选择具有相同 ID 的组的最后一个元素
- 第一个元素和最后一个元素之间的连续循环
- 在JavaScript数组中查找最后一个元素的计算效率最高的方法
- 将同一个元素追加到两个下拉列表中只会导致最后一个元素被追加
- 如何在jquery中读取Section中的最后一个元素
- 设置'字体大小'jQuery不处理所选Text的最后一个元素
- AJAX只将数组的最后一个元素传递给MVC 5控制器
- 移除嵌套数组的最后一个元素,并完成最后一个数组
- PHP:获取关联数组中最后一个元素
- jquery slideDown()遍历最后一个元素