我是编程新手,正在尝试学习如何使用JavaScript。我书中的问题是我必须使用html页面中的循环来编写程序,但它们都在同一行上。这是程序:<html><body> <script> var sheepCounted = 0; while (sheepCounted < 10) { document.write("I have counted " + sheepCounted + " sheep!"); sheepCounted++; } </script></body></html>但它返回的只是:I have counted 0 sheep!I have counted 1 sheep!I have counted 2 sheep!I have counted 3 sheep!I have counted 4 sheep!I have counted 5 sheep!I have counted 6 sheep!I have counted 7 sheep!I have counted 8 sheep!I have counted 9 sheep!(全部一行)我在这个代码上也有问题                我的第一个正确的HTML页面    <body> <h1>Hello</h1> <p>My First web page.</p> <script> var name = "Nick "; document.write("Hello, " + name); if (name.length > 7) { document.write("Wow, you have a REALLY long name!"); } else { document.write("Your name isnt very long") } </script> </body> </html>请帮我!!!!! 最佳答案 首先,不建议使用document.write。您应该进行DOM操作。但是,由于您是编程新手,所以不要这样做。HTML中的所有空格(即制表符,换行符和空格)均被截断为单个空格。为了在页面上实际换行,请使用标签<br />。或者,您可以将每个文本都设为一个段落,从语义上讲更有意义。为此,只需将文本包裹在<p>标记中,例如<p>text</p>document.write("<p>I have counted " + sheepCounted + " sheep!</p>");这也适用于您的第二个问题。只需将文本包装在 text 中如果要使用DOM操作,请执行以下代码。请注意,这是更高级的功能,可以在迈出第一步的同时使用document.write<!DOCTYPE html><html> <head> <title>Test</title> <meta charset="utf-8" /> </head> <body> <script> document.addEventListener("DOMContentLoaded", function() { for (var i = 0; i < 10; i++) { var p = document.createElement("p"); p.textContent = "I have counted " + i + " sheep!"; document.body.appendChild(p); } }); </script> </body></html>关于javascript - 我无法让我的程序走下一行,我正在使用document.write,因为它使用JavaScript在html中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28399924/
10-13 00:15