为什么javascript换行符不能在HTML中工作? [英] Why doesn't javascript newlines work inside html?

查看:659
本文介绍了为什么javascript换行符不能在HTML中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下几点:

 


<脚本type =text / javascript>
document.write('Hello \\\
World')
< / script>
< / body>
< / html>

由于您可能都知道\\\
无法正常工作,因此我必须使用< br>。
如果我链接到一个外部js文件,它将不起作用。以下是我的问题:

<1>为什么 \\\
不工作?

2)为什么甚至工作?难道不应该在脚本
标签内的所有内容都严格遵守javascript而不是html和js之间的混淆?

3)是否可以使 \\\
以某种方式工作?

4)我知道 \t 也不起作用。任何其他的东西,不会在
html文件里面工作?

5)无关的问题(我不想为此打开一个新问题)。
我安装了node.js,以便能够从vim内部试用js脚本,但在运行此脚本时
出现文档未定义错误。同样的事情发生时,我试图从repl。任何想法?

顺便说一句,我知道这些问题可能已被问及一百万次,但我真的找不到好的答案,我得到的是我应该使用< br> ,而不是 \\\
。感谢您的帮助。

解决方案

\\\
有一个排序调试器(或类似的开发人员工具),你可以看到文档来源,你会看到确实有一个换行符。问题在于你看页面的方式 - 你不是阅读它的源代码,你正在阅读它作为一个html文档。 html中的空格被压缩到一个空格中。所以,当你改变源代码时,它确实会改变,尽管当被解释为html文档时,该改变没有被显示。



你的node.js错误是最可能的由于您在服务器上运行浏览器脚本而造成的。即引用文档的脚本旨在在浏览器中运行,其中存在DOM等。尽管通用节点进程没有这样的全局对象,因为它不是浏览器。因此,当您尝试运行引用名为document的全局对象的代码时,假设它存在就像在浏览器中一样,它会引发错误。 document.write 不存在,如果要写入屏幕,请尝试 console.log 或查看其他util函数。

So I have the following:

<html>
  <body>
    <script type="text/javascript">
      document.write('Hello\nWorld')
    </script>
  </body>
</html>

As you probably all know \n doesn't work and I have to use <br> instead. It won't work either if I link to an external js file. Here are my questions:

1) Why doesn't \n work?
2) Why does <br> even work? Shouldn't everything that's inside the script tags be strictly javascript instead of a dirty mix between html and js?
3) Is it possible to make \n work somehow?
4) I know \t doesn't work either. Any other stuff that won't work inside html files?
5) Unrelated question (I didn't want to open a new question just for this). I installed node.js to be able to try out js scripts from inside vim but when I run this script I get the error "document is not defined". Same thing happens when I try from the repl. Any ideas?

Btw, I know these questions have probably been asked a million times but I honestly couldn't find good answers, all I got was that I should use &lt;br> instead of \n. Thanks for any help.

解决方案

\n works, if you have a debugger of sorts (or similar developer tool) you can see the document source, and you will see that there is indeed a newline character. The problem is the way you are looking at the page - you're not reading it's source, you're reading it as an html document. Whitespace in html is compressed into a single space. So when you change the source, it does indeed change, although when interpreted as an html document, that change isn't shown.

Your node.js error is most probably caused by the fact that you're running browser scripts on the server. I.e. scripts that refer to the document are intended to be run in a browser, where there is a DOM etc. Although a generic node process doesn't have such a global object because it isn't a browser. As such, when you try and run code that references a global object called document on the assumption that it exists just like in the browser, it will throw an error. document.write doesn't exist, if you want to write to the screen, try console.log or look into the other util functions.

这篇关于为什么javascript换行符不能在HTML中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆