简体   繁体   English

我的js数组不会在chrome浏览器中与console.log一起显示

[英]My js array wont display with console.log in chrome browser

console.log doesn't display what I want it to show in my chrome browser dev console. console.log不会显示我想要在chrome浏览器开发者控制台中显示的内容。

I have this code: 我有以下代码:

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8">

  <title></title>

 </head>

 <body>
  <script src="script.js">
       console.log(amazingCars);
       var amazingCars =["BMW","Lexus","Ford","Mercedes"]; 
       amazingCars.pop();
       console.log(amazingCars);
       amazingCars.sort();
       console.log(amazingCars);
       amazingCars.length;
       console.log(amazingCars.length);
   </script> 
 </body>

</html>

The directions for the assignment are the following: 分配说明如下:

Link the JavaScript page to the HTML page.
Create an array named amazingCars that contains four values:
    BMW
    Lexus
    Ford
    Mercedes
Remove the last value from the array
Next, sort the array in alphabetical order
Lastly, find the length of the array

I dont understand why it isn't displaying or the reason of this issue 我不明白为什么它不显示或此问题的原因

You have to change <script src="script.js"> to <script> . 您必须将<script src="script.js">更改为<script>

Alternatively you can keep it this way and create a script.js file and copy paste all the code you have inside the <script> tag to the file. 另外,您可以保持这种方式,并创建一个script.js文件,然后将<script>标记内的所有代码复制粘贴到该文件中。 This file need to be in the same folder as your index.html file. 该文件必须与index.html文件位于同一文件夹中。

Firstly, make the <script src="index.js"></script> seperate from the code in the head, also unless if you have a seperate javascript file you dont need the src part at all, and secondly the first console.log outputs it before definition, creating an error. 首先,使<script src="index.js"></script>与头部的代码分开,也除非您有单独的javascript文件,否则根本不需要src部分,其次是第一个控制台。日志在定义之前将其输出,从而产生错误。 I hope this helped 希望这对您有所帮助

If you are putting your JS code in html just add them between <script></script> tag. 如果要将JS代码放入html中,只需将它们添加在<script></script>标记之间。

You do not need to add src for script unless your JavaScript code is coming from a separate script file. 除非您的JavaScript代码来自单独的脚本文件,否则无需为脚本添加src

The src attribute specifies the URL of an external script file . src属性指定外部脚本文件URL

If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. 如果要在网站的多个页面上运行相同的JavaScript,则应创建一个外部JavaScript文件,而不是一遍又一遍地编写相同的脚本。 Save the script file with a .js extension, and then refer to it using the src attribute in the <script> tag 保存扩展名为.js的脚本文件,然后使用<script>标记中的src属性引用该文件。

.

As per the instructions that you are following : "Link the JavaScript page to the HTML page." 按照您所遵循的说明: “将JavaScript页面链接到HTML页面。”

  1. Create another file "index.js" in your project directory. 在您的项目目录中创建另一个文件“ index.js”。
  2. Move your js code into the index.js file in your project directory and then use : 将您的js代码移到项目目录中的index.js文件中,然后使用:

    <script src="index.js"></script> in your HTML. HTML中的<script src="index.js"></script>

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

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