因此,在尝试访问新创建的网页中的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。
答案 0 :(得分:0)
请参阅:Code after window.open doesn't get executed
在此处复制相应的回复:
从window.open调用返回对窗口的引用,您可以使用它来修改窗口。
win=window.open(...);
win.document.doYourThing
您可能还需要等到文档准备好(也称为已加载)。使用下面的jquery
$(win.document).ready(function() {
//the document is loaded by here, this is probably where you should do your stuff.
});
答案 1 :(得分:0)
这可能会成为你想做的伎俩。使用Javascript默认
sessionStorage
将您的代码更改为此
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 支持所有浏览器。