为什么这些 Javascript 换行技术在我的浏览器中不起作用?

发布于 2024-11-19 11:30:14 字数 667 浏览 9 评论 0原文

我是 Javascript 新手,只是想学习基础知识。这些示例都没有显示在我的浏览器中。我在这里做错了什么?

<HTML> 
<HEAD> 
<TITLE>My First JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!-- 
document.write("I love JavaScript") <br/> 
document.write("Craig Knaak") <br/>
document.write("It's a good day to die") <br/> 
document.write('He said "Welcome to my world"')

document.write("I love JavaScript\n")
document.write("Craig Knaak\n")
document.write("It's a good day to die\n")
document.write('He said "Welcome to my world"')

//-->
</SCRIPT>
</BODY>
</HTML>

I am new to Javascript, and just trying to learn the basics. Neither of these examples display in my browser. What am I doing wrong here?

<HTML> 
<HEAD> 
<TITLE>My First JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!-- 
document.write("I love JavaScript") <br/> 
document.write("Craig Knaak") <br/>
document.write("It's a good day to die") <br/> 
document.write('He said "Welcome to my world"')

document.write("I love JavaScript\n")
document.write("Craig Knaak\n")
document.write("It's a good day to die\n")
document.write('He said "Welcome to my world"')

//-->
</SCRIPT>
</BODY>
</HTML>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一向站在原地 2024-11-26 11:30:14

这些换行符必须位于带引号的字符串的内部

document.write("I love JavaScript <br/>")


不是 javascript 代码,它是一个 HTML 标记,而 document.write 会将 HTML 写入页面,因此您必须将其包含在写入字符串中。

Those line breaks have to be on the inside of the quoted string.

document.write("I love JavaScript <br/>")

the <br/> isn't javascript code, its an HTML tag, and document.write will write HTML to the page, so you have to include it inside of the write string.

﹏半生如梦愿梦如真 2024-11-26 11:30:14

您需要将 br 添加到表达式中:

document.write("I love JavaScript <br/>" )

You need to add the br into the expression:

document.write("I love JavaScript <br/>" )
掀纱窥君容 2024-11-26 11:30:14

删除
标签,你应该就可以了。

另外 \n 只是在源代码中添加了一个中断。要在页面上添加中断,请将 \n 更改为

Remove the <br/> tags and you should be good.

Also \n just adds a break to the source code. To add a break on the page change the \n to <br/>

木森分化 2024-11-26 11:30:14

基本上,您想要输出的 HTML 应该位于 javascript 字符串内。另外,不要忘记 javascript 语句末尾的 ; 。 ;)

document.write("I love JavaScript<br />");
document.write("Craig Knaak<br />");
document.write("It's a good day to die<br />");
document.write('He said "Welcome to my world"');

Basically, the HTML you want to output should be inside the javascript strings. Also, dont forget your ; at the end of your javascript statements. ;)

document.write("I love JavaScript<br />");
document.write("Craig Knaak<br />");
document.write("It's a good day to die<br />");
document.write('He said "Welcome to my world"');
双马尾 2024-11-26 11:30:14

除了已经给出的答案是完美的之外,要在字符串中插入
,我建议您使用分号来结束语句。由于在此示例中大部分情况下都可以在没有它们的情况下工作,因此有一天您会遇到问题,因为在某些情况下无法进行自动更正。

document.write("I love JavaScript<br/>"); // moves following text to the next line inside the browser
document.write("I love JavaScript\n"); // will not work inside the browser window 

第二行不会在浏览器中插入新行,因为换行符仅被解释为空格,尽管它仅在文本编辑器中将后续文本移动到新行(例如在某些浏览器的源视图中)以提高可读性。不过,了解一下还是有好处的,因为它被用于大多数需要输出文本的其他应用程序中。

Besides the already given answers are perfect, to insert <br/> in the strings, I would recommend you to use semicolons to end your statements. Since in this example will work mostly without them, you will experience problems someday because there are situations where automatic correction is not possible.

document.write("I love JavaScript<br/>"); // moves following text to the next line inside the browser
document.write("I love JavaScript\n"); // will not work inside the browser window 

The second line will not insert a new line in browsers since the newline character is interpreted as whitespace only, though it moves following text to a new line in text editors only (e.g. in source view of certain browsers) for better readability. Still it is good to know since it is being used in most other applications where you need to output text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文