- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
index.html
和 scripts.js
如果我点击错误的答案......
is-false
类和 quiz__info
如果我点击正确答案...
is-false
类var score = 0;
$(document).on("click", ".quiz__response", function(){
$(this).siblings().addBack().addClass("is--unclickable");
$(this).siblings().show("quiz__info"); // Show extra info
console.log("Clicked");
if ($(this).hasClass("answer--true")) {
$(this).addClass("is--true");
$(this).find("i").show();
$(this).siblings().find("i").show();
// Update score
score++
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
} else {
// $(this).addClass("is--false");
$(this).siblings().addClass("is--false");
$(this).find("i").show();
$(this).siblings().find("i").show();
}
});
<div class="quiz__question question-5 question__sports">
<h3>Question #5</h3>
<p class="quiz__description">A former PGA Tour winner from Brandon passed away at the age of 63. Can you name him?</p>
<div class="quiz__responses">
<button class="quiz__response response-5 answer--false">Billy Casper <i class="fa fa-times"></i></button>
<button class="quiz__response response-5 answer--false">Rory Woods <i class="fa fa-times"></i></button>
<button class="quiz__response response-5 answer--true">Dan Halldorson <i class="fa fa-check"></i></button>
<button class="quiz__response response-5 answer--false">Billy Hurley <i class="fa fa-times"></i></button>
<div class="quiz__info">
<p><span class="answer">A: </span>Halldorson competed for nearly 30 years on the PGA Tour, winning the 1980 Pensacola Open. He also captured the 1986 Deposit Guaranty Golf Classic, which was an unofficial tour stop. All told, he had 28 top 10 finishes in 431 PGA Tour events, pocketing nearly $1.2 million in career earnings.</p>
</div>
</div> <!-- .responses -->
</div> <!-- .question -->
我有一个包含 25 个问题的测验,每个问题有 4 个可能的答案 .quiz__response
。单击按钮时,我希望执行以下操作,但仅针对与该特定问题相关的答案,现在它将以下内容应用于所有问题。
单击按钮时...
addClass(".is--unclickable")
这四个答案.fa-check
如果答案为 true,.fa-times
如果答案为 falseaddClass(".is--true")
将背景更改为绿色,addClass(".is--false")
进行更改其余三个答案的背景为红色$(function(){
/*-------------------------------------
QUIZ
--------------------------------------*/
function showScoreBox() {
var scrollDepth = $(window).scrollTop();
var divPosition = $(".quiz__header").offset().top - 45;
var windowWidth = $(window).width();
// console.log(windowWidth);
if (scrollDepth > divPosition && (windowWidth > 768)) {
$(".quiz__score").show();
$(".quiz__score--mobile").hide();
} else {
$(".quiz__score").hide();
$(".quiz__score--mobile").show();
}
} showScoreBox();
$(window).on("scroll", function(){
showScoreBox();
});
$(window).on("resize", function(){
showScoreBox();
});
var score = 0;
$(".quiz__response").on("click", function(){
$(".quiz__response").addClass("is--unclickable");
$(".quiz__info").show("quiz__info"); // Show extra info
console.log("Clicked");
if ($(this).hasClass("answer--true")) {
$(this).addClass("is--true");
$(".fa-check").show();
// Update score
score++
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
} else {
$(this).addClass("is--false");
$(".fa-times").show();
}
});
/*-------------------------------------
RESET
--------------------------------------*/
function resetQuiz() {
$(".quiz__response").removeClass("answer--true answer--false");
var score = 0
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
}
$(".button__reset").on("click", function(){
resetQuiz();
});
/*-------------------------------------
CONFETTI
--------------------------------------*/
function rainConfetti() {
if (score === 25) {
canvas.show()
}
}
/*-------------------------------------
SMOOTH SCROLL
--------------------------------------*/
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<div class="quiz">
<div class="quiz__score">
<p><span class="quiz__correct">0</span> / 25</p>
</div>
<h2 class="quiz__header" id="top">Take the quiz</h2>
<p class="credit">How well have you been paying attention to what's been happening in southwestern Manitoba in 2015? Take our 25-question quiz to test your local news knowledge.</p>
<div class="quiz__questions">
<!-- <canvas id="confetti"></canvas> -->
<!-- Question -->
<div class="quiz__question question-1 question__sports">
<div class="wrapper">
<!-- <img src="http://placehold.it/500x300" alt="" class="quiz__image"> -->
</div>
<h3>Question #1</h3>
<p class="quiz__description">What Brandon Wheat King, who was drafted by the New York Islanders, lost his desire to play hockey and walked away from the team prior to this season starting?</p>
<div class="quiz__responses">
<button class="quiz__response answer--false">Kale Clague <i class="fa fa-times"></i></button>
<button class="quiz__response answer--false">Macoy Erkamps <i class="fa fa-times"></i></button>
<button class="quiz__response answer--true">Ryan Pilon <i class="fa fa-check"></i></button>
<button class="quiz__response answer--false">Colin Cloutier <i class="fa fa-times"></i></button>
<!-- The six-foot-two, 212pound rearguard had 11 goals and 41 assists in 68 regular season games, with a goal and 11 assists in 19 playoff contests last season, paired mainly with Russian Ivan Provorov. In 193 regularseason games with Brandon and the Lethbridge,Hurricanes, Pilon has 23 goals, 93 assists and 116 penalty minutes. -->
</div> <!-- .responses -->
</div> <!-- .question -->
<!-- Question -->
<div class="quiz__question question-2 question__sports">
<div class="wrapper">
<!-- <img src="http://placehold.it/500x300" alt="" class="quiz__image"> -->
</div>
<h3>Question #2</h3>
<p class="quiz__description">Which former Brandon skip refused to talk to the local media after losing his fifth provincial men’s curling championship game in six years at Westman Place?</p>
<div class="quiz__responses">
<button class="quiz__response answer--false">Russ Howard <i class="fa fa-times"></i></button>
<button class="quiz__response answer--false">Kerry Burtnyk <i class="fa fa-times"></i></button>
<button class="quiz__response answer--true">Mike McEwen <i class="fa fa-check"></i></button>
<button class="quiz__response answer--false">Braden Calvert <i class="fa fa-times"></i></button>
</div> <!-- .responses -->
</div> <!-- .question -->
<!-- McEwen’s Winnipeg based team of B.J. Neufeld, Matt Wozniak and Denni Neufeld lost to Ried Carruthers 53 at the Safeway Championship hosted by Brandon. -->
</div> <!-- quiz -->
最佳答案
首先,您可以使用 event delegation [直接事件和委托(delegate)事件部分] 绑定(bind)单个事件处理程序,而不是为每个按钮绑定(bind)一个事件处理程序。
$(".quiz__response").on("click", function(){
$(document).on("click", ".quiz__response", function(){
要仅在与特定问题相关的答案上添加 .is--unclickable
,请更改$(".quiz__response").addClass("is--unclickable");
到$(this).siblings().addBack().addClass("is--unclickable");
或$(this).parent().children().addClass("is--unclickable");
要显示 fa-times
或 fa-check
,请更改$(".fa-check").show();
$(".fa-times").show();
到$(this).find("i").show();
(同时在 if 和 else 语句中)
并在 if 语句中添加 $(this).siblings().addClass("is--false");
,将剩余的答案涂成红色,如果检查正确的。
完整的 js 更改
$(document).on("click", ".quiz__response", function() {
$(this).siblings("button").addBack().addClass("is--unclickable");
$(this).siblings(".quiz__info").show(); // Show extra info
console.log("Clicked");
if ($(this).hasClass("answer--true")) {
$(this).addClass("is--true");
$(this).siblings("button").addBack().find("i").show();
$(this).siblings("button").addClass("is--false");
// Update score
score++
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
} else {
$(this).addClass("is--false");
$(this).find("i").show();
}
});
关于javascript - 定位测验中的按钮组,而不是点击时的所有测验问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560214/
我知道这曾经是一个话题,但搜索后我找不到答案 - 也许我的问题太基本了。不过,我正在创建一个 html 测验 - 5 页,分数从一页到下一页,然后在最后评分。 这是我正在尝试使用的代码,但它根本不起作
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在做一个简单的 php 问答游戏。我将问题和答案存储在数据库中。我的 tblQuiz 的表结构是这样的: _id, question, answer1, answer2, answer3, cor
我在考试中遇到了这个问题。 以下不正确 javascript 代码的输出是什么?错误是什么? num=0; if (num > 10); { num = num + 10; } alert(n
我在 JavaScript 中验证 4 个问题多项选择测验时似乎遇到了一些问题。现在,如果没有选择任何答案,则会弹出警报,但即使只选择了一个答案,警报也不会弹出。我正在将我的函数作为提交按钮的 onc
我正在尝试使用 JavaScript 构建一个问答游戏。 测验答案存储在变量中,如下所示: var correctAnswer = $('#createCorrectAnswer').val(); 然
下面我做了一个小测验,有 4 个问题,问题 2,3 和 4 被 CSS 隐藏了,但是你可以尝试从 CSS 中删除样式 (".pytja2, .pytja3, .pytja4 { display: no
private void startGame() { System.out.println("Exercises must be completed as quickly as pos
这听起来像是我之前问过的问题,但它只得到了部分回答。所以我想从我提出的问题开始。然后,一旦到达最后一个问题(#5),它就会返回到第一个问题(#1),当到达起始问题时,它将停止。我尝试在以下代码中执行此
愚蠢的问题,但我的教授不回复电子邮件,所以我在这里问。我现在正在复习期末考试,并且正在复习期中考试来学习。现在,这真的很愚蠢,因为我两个月前就答对了这个问题。但对于我的一生来说,现在回想起来,我无法弄
我无法编写一个循环来计算我为我的 wordpress 网站创建的测验的分数。 测验样式是“将 A 列中的项目与 B 列中的项目进行匹配”。 B 列项目使用选择元素来匹配 A 列中的相应项目。 我能想到
为什么选择不正确单选选项时,if语句中的else仍然出现? else 部分只应在未选择单选按钮时出现。真正的结果是下一个问题和可能的答案出现。这是 jsfiddle .我做错了什么? function
我正在尝试为西类牙语类(class)创建测验。我对 JavaScript 经验不多,但对 html 和 CSS 相当精通。我有一个问题,然后是三个带答案的单选按钮。有两个错误答案和一个正确答案。我总共
我正在编写一个允许用户设置个人测验的程序(如记事卡式系统)。参加测验时,用户会在每个问题上计时。虽然答案不会是多项选择,但它们将由用户输入。 有没有一种方法可以让我充分接受那些不是他们第一次设置测验时
我正在尝试根据找到的教程创建一个简单的测验。 http://css-tricks.com/building-a-simple-quiz/ 不幸的是,这让我无法自拔,答案可能非常简单。 我让这个工作完美
最近我遇到了以下测验。想象一下我们有这张 table +--------+ | colors | +--------+ | red | | black | | white | | green
这里的 W3Schools 网站上发布了一个 jQuery 测验... http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery 问题#1
我正在写一个简单的在线测验。有四个单选按钮作为选择。最初,脚本仅填充第一个问题的选项。之后,用户应该选择下一个并单击按钮。如果答案与正确的匹配,则总数递增。否则,将加载下一个问题。提交第 10 个问题
我不是 Haskell 程序员,但我对以下问题感到好奇。 非正式函数规范: 令 MapProduct 为一个函数,它采用名为 F 的函数和多个列表。它返回一个列表,其中包含使用每个可能组合中每个列表中
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!