简体   繁体   English

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

[英]I get a blank page on Firefox (only h2 showing)

This is the page written, and I should be getting "I can print", but I only get the title, and the rest of the page is blank. 这是写的页面,我应该得到“我可以打印”的信息,但是我只得到标题,其余页面为空白。 Where is the mistake? 错误在哪里?

<!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>

console.log() is not what is used to add content to the page. console.log()不是用来向页面添加内容的内容。 There are several ways of solving your problem - I have added one of those as a code snippet: 有几种方法可以解决您的问题-我已添加其中一种作为代码段:

 function PrintStuff(myDocuments) { this.documents = myDocuments; } PrintStuff.prototype.print = function() { var paragraph = document.createElement("p"); paragraph.innerText = this.documents; document.body.appendChild(paragraph); console.log(this.documents); // this only prints to the browser console (and throws an exception in IE if the console was not opened before) } var newObj = new PrintStuff("I can print"); newObj.print(); 
 <h2>Object exercise 4</h2> 

If you do not know what the browser console is, press [F12] in the browser (it is really helpful) or see What are browser developer tools? 如果您不知道浏览器控制台是什么,请在浏览器中按[F12](这确实很有用),或者请参阅什么是浏览器开发人员工具? (all the stuff at developer.mozilla.org is awesome). (developer.mozilla.org上的所有内容都很棒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM