Javascript 我在Firefox上看到一个空白页面(仅显示h2)

Javascript 我在Firefox上看到一个空白页面(仅显示h2),javascript,html,Javascript,Html,这是写的页面,我应该得到“我可以打印”,但我只得到标题,页面的其余部分是空白的。错在哪里 <!DOCTYPE html> <html> <head> <title>Object exercise 4</title> </head> <body> <h2>Object exercise 4</h2> <script type = "text/javascript"> funct

这是写的页面,我应该得到“我可以打印”,但我只得到标题,页面的其余部分是空白的。错在哪里

<!DOCTYPE html>
<html>
<head>
<title>Object exercise 4</title>
</head>
<body>
<h2>Object exercise 4</h2>
<script type = "text/javascript">

function PrintStuff(myDocuments)
{
    this.documents = myDocuments;
}

PrintStuff.prototype.print=function()
{
console.log(this.documents);
}

var newObj = new PrintStuff("I can print");
newObj.print();

</script>
</body>
</html>

目标练习4
目标练习4
函数PrintStuff(myDocuments)
{
this.documents=myDocuments;
}
PrintStuff.prototype.print=function()
{
console.log(this.documents);
}
var newObj=新的打印材料(“我可以打印”);
newObj.print();
console.log()不是用于向页面添加内容的工具。有几种方法可以解决您的问题-我添加了其中一种作为代码片段:

函数PrintStuff(myDocuments){
this.documents=myDocuments;
}
PrintStuff.prototype.print=函数(){
var段落=document.createElement(“p”);
段落.innerText=this.documents;
文件.正文.附件(第段);
console.log(this.documents);//这仅打印到浏览器控制台(如果以前未打开控制台,则会在IE中引发异常)
}
var newObj=新的打印材料(“我可以打印”);
newObj.print()

Object exercise 4
您正在打印到控制台,而不是HTML页面。浏览器不显示脚本标记内的内容。。。。因此,您通常只在屏幕上打印H2内容。您可能希望将
console.log(this.documents);
更改为
document.write(this.documents);
或创建一个新的HTML元素,如
,然后放置“我可以打印”“好了,弗雷金诺德,我试过了,完全有道理,但不起作用。我基本上只加了三行。我删除了代码片段的大部分html,因为不需要它来演示您要查找的内容。如果只将PrintStuff.prototype.print()中的三行新行添加到代码中,应该可以正常工作。请确保浏览器未使用未经更改的缓存副本。并使用浏览器的开发工具([F12])分析页面或在控制台中查找异常。@Yozex哪个部分不工作?Gordian上面的代码片段工作得非常好(我现在正在使用IE11)。您是否在console/dev工具中发现错误?好的,我已将其完全重新键入到一个新文件中,现在它可以工作了。在以前的版本中,我在现有文件上做了一些复制粘贴,并以不同的名称保存了它。可能它确实与缓存有关。