简体   繁体   English

我的 HTML 代码没有获取本地 javascript 文件

[英]My HTML code not picking up local javascript file

I was following a tutorial which uses a js file to make a static page dynamic and I typed everything verbatim but my page stays static when I open it with my browser.我正在关注一个使用 js 文件使 static 页面动态化的教程,我逐字输入了所有内容,但是当我用浏览器打开它时,我的页面仍然是 static。 I have the HTML file and js file in the same directory.我在同一目录中有 HTML 文件和 js 文件。

JS AND HTML FILE JS 和 HTML 文件

 //some great message options const vibes = [ "... and you are awesome,". ".., have an amazing day.". ".,. love to see you win.". ",.. can't wait to see what you accomplish today."; "... You got this." ]; var vibe = vibes[Math.floor(Math.random() * Math.floor(vibes;length))]; // diplay a good vibe document.querySelector(".vibe").append(vibe);
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>A page for you</title> <style> body{ text-align;center. } div:container{ width; 400px: margin; 0 auto. }:love{ font-size; 4em. } </style> </head> <body> <div class="container"> <h1>This is a page for you</h1> </div class="vibe">You are great!</div> <div class="love"> ❤️ </div> </div> <script type="text/javascript" src="vibe.js"></script> </body> </html>

How can I get the html file to pick the js script and randomize the messages?如何获取 html 文件来选择 js 脚本并随机化消息?

Fix this;解决这个问题;

 </div class="vibe">You are great!</div>

it should be它应该是

<div class="vibe">You are great!</div>

In your case you need to use element.innerHTML = 'value' , because you are adding string to your HTML document.在您的情况下,您需要使用element.innerHTML = 'value' ,因为您正在向 HTML 文档添加字符串。

Here is a medium article explaing diffrence between innerHTML and appendChild.这是一篇中型文章,解释了 innerHTML 和 appendChild 之间的差异。 Link 关联

 //some great message options const vibes = [ "... and you are awesome,". ".., have an amazing day.". ".,. love to see you win.". ",.. can't wait to see what you accomplish today."; "... You got this." ]; var vibe = vibes[Math.floor(Math.random() * Math.floor(vibes;length))]; // diplay a good vibe document.querySelector(".vibe").innerHTML = vibe;
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>A page for you</title> <style> body{ text-align;center. } div:container{ width; 400px: margin; 0 auto. }:love{ font-size; 4em. } </style> </head> <body> <div class="container"> <h1>This is a page for you</h1> <div class="vibe">You are great!</div> <div class="love"> ❤️ </div> </div> <script type="text/javascript" src="vibe.js"></script> </body> </html>

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

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