无法在javascript中为新创建的页面中的div分配文本
因此,在尝试访问新创建的网页中的DOM时,我遇到了一个问题。基本上,该函数应该做的是加载新网页,并将questionpage.html页面中的div“question_text”分配给我希望它显示的文本。但是当我加载页面时,没有显示任何内容。我已经附上了我所有的javascript代码无法在javascript中为新创建的页面中的div分配文本,javascript,html,css,Javascript,Html,Css,因此,在尝试访问新创建的网页中的DOM时,我遇到了一个问题。基本上,该函数应该做的是加载新网页,并将questionpage.html页面中的div“question_text”分配给我希望它显示的文本。但是当我加载页面时,没有显示任何内容。我已经附上了我所有的javascript代码 /** * Created by Alex on 24-Nov-15. */ $(function() { var questions = new array; var count
/**
* 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。请参见:
在此处复制相应的响应:
从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.
});
这可能会达到你想要的效果。使用Javascript默认值
会话存储
将您的代码更改为
function createWebpage() {
sessionStorage.setItem("question", questions[count].questionText);
window.open('../src/questionpage.html','_self');
}
在questionpage.html页面上检索会话存储值
$(document).ready(function(){
var received = sessionStorage.getItem("question");
document.getElementById("question_text").innerHTML = received;
});
阅读有关会话存储的更多信息
支持所有浏览器。i未定义,您缺少一个循环。您从何处调用createWebpage?从我网站起始页的按钮调用createWebpage,我希望它加载新页面并分配divs值。我认为这是原因:请同时显示HTML。