gpt4 book ai didi

javascript - 无法将文本分配给在 javascript 中新创建的页面中的 div

转载 作者:行者123 更新时间:2023-11-28 06:48:46 25 4
gpt4 key购买 nike

所以我在尝试访问新创建的网页中的 DOM 时遇到了问题。基本上该功能应该做的是加载新网页并将页面 questionpage.html 中的 div“question_text”分配给我希望它显示的文本。但是当我加载页面时,没有任何显示。我附上了我所有的javascript代码。

    /**
* Created by Alex on 24-Nov-15.
*/
$(function() {


var questions = new array;
var count = 0;
addQuestions();

function Question(questionText, possibleAnswers, chosenAnswer) {
this.questionText = questionText;
this.possibleAnswers = possibleAnswers;
this.chosenAnswer = chosenAnswer;
}

function addQuestions() {


var question1 = new Question(
"What is your first name",
['Alex','Angelo'],
""
);

var question2 = new Question(
"What is your surname",
['Latham','Poupard'],
""
);

var question3 = new Question(
"What is your date of birth",
['November 93','March 94'],
""
);

var question4 = new Question(
"What is your postcode"
['G51 1HF', 'G84 8NW'],
""
);



questions.push(question1);
questions.push(question2);
questions.push(question3);
questions.push(question4);
}





});

function createWebpage() {

window.open('../src/questionpage.html','_self');
document.getElementById("question_text").innerHTML = questions[count].questionText;


}

底部的这个函数试图将文本分配给 div。

最佳答案

这可能会达到您想要的效果。默认使用 JavascriptsessionStorage 把你的代码改成这个

function createWebpage() {

sessionStorage.setItem("question", questions[count].questionText);
window.open('../src/questionpage.html','_self');
}

在 questionpage.html 页面上获取 sessionStorage 值

$(document).ready(function(){
var received = sessionStorage.getItem("question");
document.getElementById("question_text").innerHTML = received;
});

阅读更多关于 sessionStorage

http://www.w3schools.com/html/html5_webstorage.asp

https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage支持所有浏览器。

关于javascript - 无法将文本分配给在 javascript 中新创建的页面中的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33901523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com