编码挑战:JavaScript 向网页添加一个问题列表,可以通过点击按钮显示答案

问题描述

以下是起始HTML:

<html>
 <head>
 </head>
 <body>
		<div id="content"></div>
 </body>
</html>

 js代码:

// Complete-This-Code

// List of questions (statement + answer)
const questions = [
  {
    statement: "2+2?",
    answer: "2+2 = 4"
  },
  {
    statement: "In which year did Christopher Columbus discover America?",
    answer: "1492"
  },
  {
    statement:
      "What occurs twice in a lifetime, but once in every year, twice in a week but never in a day?",
    answer: "The E letter"
  }
];

// Write - Your - Code - From - Here

        补充完成JavaScript 代码以在 div 中显示问题,每个问题旁边都有一个按钮,上面写着“Show the answer”,点击此按钮会替换为该问题的答案。

最终界面如下:

 解决方法

我的:

// Complete-This-Code

// List of questions (statement + answer)
const questions = [
  {
    statement: "2+2?",
    answer: "2+2 = 4"
  },
  {
    statement: "In which year did Christopher Columbus discover America?",
    answer: "1492"
  },
  {
    statement:
      "What occurs twice in a lifetime, but once in every year, twice in a week but never in a day?",
    answer: "The E letter"
  }
];

// Write - Your - Code - From - Here
const docElement=document.getElementById("content");
for(let i =0;i<questions.length;i++)
{
  const button=document.createElement("button");
  const qElement=document.createElement("p");
  const answerElement=document.createElement("p");
  answerElement.textContent=questions[i].answer;
  qElement.textContent=`Question ${i+1}:`+questions[i].statement;
  button.textContent="Show answer";
  button.addEventListener("click",e=>{
     docElement.replaceChild(answerElement,button);
  });
  docElement.appendChild(qElement);
  docElement.appendChild(button);
}

显示效果:

 

 参考答案:

// Complete-This-Code

// List of questions (statement + answer)
const questions = [
  {
    statement: "2+2?",
    answer: "2+2 = 4"
  },
  {
    statement: "In which year did Christopher Columbus discover America?",
    answer: "1492"
  },
  {
    statement:
      "What occurs twice in a lifetime, but once in every year, twice in a week but never in a day?",
    answer: "The E letter"
  }
];

let i = 1; // Question index

questions.forEach(question => {
  // Question title
  const titleElement = document.createElement("strong");
  titleElement.textContent = `Question ${i}: `;

  // Question statement text in a <i> tag
  const statementTextElement = document.createElement("i");
  statementTextElement.textContent = question.statement;

  // Question statement in a <div> tag
  const statementElement = document.createElement("div");
  statementElement.appendChild(titleElement);
  statementElement.appendChild(statementTextElement);

  // Answer zone, with a button initially
  const answerZoneElement = document.createElement("div");
  const buttonElement = document.createElement("button");
  buttonElement.textContent = "Show answer";
  answerZoneElement.appendChild(buttonElement);

  buttonElement.addEventListener("click", () => {
    // Replace button by answer
    const answerElement = document.createElement("div");
    answerElement.textContent = question.answer;
    answerZoneElement.innerHTML = "";
    answerZoneElement.appendChild(answerElement);
  });

  // Question is statement + answer
  const questionElement = document.createElement("p");
  questionElement.appendChild(statementElement);
  questionElement.appendChild(answerZoneElement);
  document.getElementById("content").appendChild(questionElement);

  i++;
});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值