简体   繁体   English

index.html 不会在 Chrome 浏览器中显示 main.js 代码

[英]index.html won't show main.js code in chrome browser

I was following along with a tutorial on how to show .js files on an index.html but javascript only shows when it's part of the index.html code rather than on a separate .js file.我正在关注如何在 index.html 上显示 .js 文件的教程,但 javascript 仅在它是 index.html 代码的一部分而不是单独的 .js 文件时才显示。 Chomre dev tools is picking up a syntaxerror though I'm not sure what it is.尽管我不确定它是什么,但 Chomre 开发工具发现了一个语法错误。 I have even tried opening up other people's github js work and have had no luck我什至尝试打开其他人的 github js 工作,但没有运气

I have checked browser restrictions我检查了浏览器限制

//main.js code from jsfile 
<script>
  document.body.innerHTML = "This is some content";
</script>

//html uses  src="js/main.js" in a script tag after <body> tag

Should show "This is some content" on web page应该在网页上显示“这是一些内容”

When you put JS code in a separate file, do not include the <script> tags;将 JS 代码放在单独的文件中时,不要包含<script>标签; just the JavaScript:只是 JavaScript:

// main.js
document.body.innerHTML = "This is some content";

Not like this:不是这样的:

// main.js
<script>
  document.body.innerHTML = "This is some content";
</script>

The <script> tags are used to separate the HTML/CSS from JS in an .htm file. <script>标签用于将 HTML/CSS 与 .htm 文件中的 JS 分开。 A .js file should not have any HTML/CSS in it. .js 文件中不应包含任何 HTML/CSS。

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

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