ajax实现上下题目的切换,【Web前端问题】js实现点击上一题和下一题出现对应的题目,第一题和最后一题改变按钮的内容。...

使用了bootstrap框架了面的tabs,可以点击上面的题数切换题目,求指导

回答:

你可以参考一下这个

希望能给你带来思路

测验表2

@import url(https://fonts.googleapis.com/css?family=Work+Sans:300,600);

body{

font-size: 20px;

font-family: 'Work Sans', sans-serif;

color: #333;

font-weight: 300;

text-align: center;

background-color: #f8f6f0;

}

h1{

font-weight: 300;

margin: 0px;

padding: 10px;

font-size: 16px;

background-color: #444;

color: #fff;

}

.question{

font-size: 30px;

margin-bottom: 10px;

}

.answers {

margin-bottom: 20px;

text-align: left;

display: inline-block;

}

.answers label{

display: block;

margin-bottom: 10px;

}

button{

font-family: 'Work Sans', sans-serif;

font-size: 16px;

background-color: #279;

color: #fff;

border: 0px;

border-radius: 3px;

padding: 10px;

cursor: pointer;

margin-bottom: 20px;

margin-top: 20px;

}

button:hover{

background-color: #38a;

}

.slide{

position: absolute;

left: 0px;

top: 0px;

width: 100%;

z-index: 1;

opacity: 0;

transition: opacity 0.5s;

}

.active-slide{

opacity: 1;

z-index: 2;

}

.quiz-container{

position: relative;

height: 200px;

margin-top: 40px;

}

测试表

前一题

下一题

提交

(function() {

const myQuestions = [

{

question: "“大煮干丝”是哪个菜系的代表菜之一( )。",

answers: {

A: "四川菜系",

B: "山东菜系",

C: "广东菜系",

D: "淮扬菜系"

},

correctAnswer: "D"

},

{

question: "红茶属于( )茶。",

answers: {

A: "半发酵",

B: "发酵",

C: "不发酵",

D: "微发酵"

},

correctAnswer: "A"

},

{

question: "满汉全席起兴于( )。",

answers: {

A: "清代",

B: "唐代",

C: "宋代",

D: "两汉"

},

correctAnswer: "A"

}

];

function buildQuiz() {

// we'll need a place to store the HTML output

const output = [];

// for each question...

myQuestions.forEach((currentQuestion, questionNumber) => {

// we'll want to store the list of answer choices

const answers = [];

// and for each available answer...

for (letter in currentQuestion.answers) {

// ...add an HTML radio button

answers.push(

`

${letter} :

${currentQuestion.answers[letter]}

`

);

}

// add this question and its answers to the output

output.push(

`

${currentQuestion.question}
${answers.join("")}
`

);

});

// finally combine our output list into one string of HTML and put it on the page

quizContainer.innerHTML = output.join("");

}

function showResults() {

// gather answer containers from our quiz

const answerContainers = quizContainer.querySelectorAll(".answers");

// keep track of user's answers

let numCorrect = 0;

// for each question...

myQuestions.forEach((currentQuestion, questionNumber) => {

// find selected answer

const answerContainer = answerContainers[questionNumber];

const selector = `input[name=question${questionNumber}]:checked`;

const userAnswer = (answerContainer.querySelector(selector) || {}).value;

// if answer is correct

if (userAnswer === currentQuestion.correctAnswer) {

// add to the number of correct answers

numCorrect++;

// color the answers green

answerContainers[questionNumber].style.color = "lightgreen";

} else {

// if answer is wrong or blank

// color the answers red

answerContainers[questionNumber].style.color = "red";

}

});

// show number of correct answers out of total

resultsContainer.innerHTML = `你答对了${myQuestions.length}中的${numCorrect}`;

}

function showSlide(n) {

slides[currentSlide].classList.remove("active-slide");

slides[n].classList.add("active-slide");

currentSlide = n;

if (currentSlide === 0) {

previousButton.style.display = "none";

} else {

previousButton.style.display = "inline-block";

}

if (currentSlide === slides.length - 1) {

nextButton.style.display = "none";

submitButton.style.display = "inline-block";

} else {

nextButton.style.display = "inline-block";

submitButton.style.display = "none";

}

}

function showNextSlide() {

showSlide(currentSlide + 1);

}

function showPreviousSlide() {

showSlide(currentSlide - 1);

}

const quizContainer = document.getElementById("quiz");

const resultsContainer = document.getElementById("results");

const submitButton = document.getElementById("submit");

// display quiz right away

buildQuiz();

const previousButton = document.getElementById("previous");

const nextButton = document.getElementById("next");

const slides = document.querySelectorAll(".slide");

let currentSlide = 0;

showSlide(0);

// on submit, show results

submitButton.addEventListener("click", showResults);

previousButton.addEventListener("click", showPreviousSlide);

nextButton.addEventListener("click", showNextSlide);

})();

回答:

1.如果数据是死的,静态的,可以使用路由功能。只是这个方法比较笨,上一题和下一题标签要出现在每个页面上。

2.如果数据是后台获取的,那就简单了,中间那一部分用路由,把id当作传递参数,每点击一次下一题,id+1

回答:

熟练地使用ajax之后这种问题可能就简单了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些指导。首先,您需要安装Node.js和Vue.js。然后,您可以按照以下步骤实现登录的前端页面和接口: 1. 创建一个Vue.js项目,在命令行中使用以下命令: ``` vue create login-app ``` 2. 在项目中创建一个登录页面组件,可以使用Vue.js提供的模板语法和组件生命周期钩子。例如,您的组件可以包括一个表单,用户可以在表单中输入用户名和密码。 3. 在Node.js中创建一个登录接口,该接口将接收来自前端的用户名和密码,并与后端数据库进行验证。如果验证成功,后端将返回一个JSON Web Token(JWT)给前端。 4. 在Vue.js中使用ajax发送请求来调用登录接口。您可以使用Vue.js提供的axios库来发送请求。例如,您可以在登录页面组件中使用以下代码: ``` import axios from 'axios'; export default { data() { return { username: '', password: '' } }, methods: { login() { axios.post('/api/login', { username: this.username, password: this.password }) .then(response => { // 将JWT保存到本地存储中 localStorage.setItem('token', response.data.token); }) .catch(error => { console.error(error); }); } } } ``` 在上面的代码中,`axios.post()`方法将发送POST请求到`/api/login`接口,并将用户名和密码作为请求体发送。如果请求成功,`response.data.token`将包含从后端接收到的JWT,您可以将其保存到本地存储中以备将来使用。 请注意,这只是一个简单的示例,用于说明如何使用Vue.js和Node.js创建登录页面和接口。在实际项目中,您需要进行更多的验证和安全性措施,以确保您的应用程序是安全的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值