简体   繁体   English

无法让我的Javascript添加或删除类

[英]Can't get my Javascript to add or remove class

I'm creating a simple quiz that asks users what kind of car they should buy. 我正在创建一个简单的测验,询问用户应该购买哪种汽车。 I can't get the quiz to show one question at a time like I want it to. 我无法像我希望的那样一次显示一个问题。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 The full code is on this codepen 完整的代码在此codepen上

I'm sure the problem is this part of the code though. 我确定问题出在代码的这一部分。 Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

//question counter
var question = 1;
maxQuestions = 4;

//event handler for click event
var nextButton = document.getElementById('next');
nextButton.onClick = function() {
  question++;
  if (question === maxQuestions) {
    //hide the next button
    $(nextButton).addClass("hidden");
  }
  //hide the last question  
  $('question' + (question - 1)).addClass("hidden");
  //show the current question  
  $('question' + question).removeClass("hidden");  
};

var prevButton = document.getElementById('prev');
prevButton.onClick = function() {
  question--;
  if (question === 1) {
    //hide the prev button
    $(prevButton).addClass("hidden");
  }
  //hide current question
  $('question' + (question + 1)).addClass("hidden");
  //show the last question
  $('question' + question).removeClass("hidden");
};

//show submit
if (question === maxQuestions) {
  $('submit').removeClass("hidden");
};

Hello it looks like there are 2 things that need to get updated 您好,看来有两件事需要更新

  1. Added '#' to your element selector 在您的元素选择器中添加了“#”
  2. using onclick instead of onClick 使用onclick而不是onClick

here is a link to the update in codepen https://codepen.io/anon/pen/PmpQxo 这是Codepen中更新的链接https://codepen.io/anon/pen/PmpQxo

//question counter
var question = 1;
maxQuestions = 4;

//event handler for click event
var nextButton = document.getElementById('next');
nextButton.onclick = function() {
  question++;
  if (question === maxQuestions) {
    //hide the next button
    $(nextButton).addClass("hidden");
  }
  console.log('next', question);
  //hide the last question  
  $('#question' + (question - 1)).addClass("hidden");
  //show the current question  
  $('#question' + question).removeClass("hidden");  
};

var prevButton = document.getElementById('prev');
prevButton.onclick = function() {
  question--;
  if (question === 1) {
    //hide the prev button
    $(prevButton).addClass("hidden");
  }
  //hide current question
  $('#question' + (question + 1)).addClass("hidden");
  //show the last question
  $('#question' + question).removeClass("hidden");
};

//show submit
if (question === maxQuestions) {
  $('#submit').removeClass("hidden");
};

You're using jQuery already, so you don't necessarily have to keep track of the questions with indexes. 您已经在使用jQuery,因此不必一定要跟踪带有索引的问题。 jQuery provides .siblings() , .next() , and .prev() to help navigate. jQuery提供.siblings() .next().prev()来帮助导航。

For example a quick solution that uses those instead of indices could look like this 例如,使用那些索引而不是索引的快速解决方案可能如下所示

https://codepen.io/anon/pen/mmWXeQ?editors=1111 https://codepen.io/anon/pen/mmWXeQ?editors=1111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM