简体   繁体   English

Javascript 运行不正确 - 尝试了一切

[英]Javascript not Running Correctly - Tried Everything

My javascript only has one line of code, and it gets the "getElementById null" error.我的 javascript 只有一行代码,它得到“getElementById null”错误。

document.getElementById("menu-background").style.left = "0";

HTML: HTML:

<html>
  <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="main.css" />
    <script type="text/javavascript" src="js/main-menu.js"></script>
  </head>
  <body>
    <div id="menu-background">
      <div id="main-menu"></div>
    </div>
  </body>
</html>

CSS: CSS:

#menu-background {
  position: fixed;
  width: 100%;
  height: 100%;
  background-image: url("images/background.png");
  background-position: center;
  background-size: cover;
  top: 0;
  text-align: center;
  left: 100%;
}
#main-menu {
  width: 60vw;
  height: 80vh;
  border-style: solid;
  border-radius: 1.125em;
  border-color: #b88a00;
  margin: auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(0, 0, 0, 0.75);
  transition: all 0.25s;
}

I run visual studio code and have checked the link to the js.我运行 Visual Studio 代码并检查了 js 的链接。 I have also tried to run it inside of the HTML file.我还尝试在 HTML 文件中运行它。

You can try this:你可以试试这个:

window.addEventListener('DOMContentLoaded', function(e) {
    document.getElementById("menu-background").style.left = "0";    
});

You should put your script at the end of the document in order for the element to be defined before reaching it:您应该将script放在文档的末尾,以便在到达之前定义元素:

<html>
  <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="main.css" />
  </head>
  <body>
    <div id="menu-background">
      <div id="main-menu"></div>
    </div>
    <script type="text/javavascript" src="js/main-menu.js"></script>
  </body>
</html>

I have found the problem!!!我发现问题了!!!

My HTML file was named "main.html" and when I renamed it to "index.html" it worked!我的 HTML 文件被命名为“main.html”,当我将它重命名为“index.html”时它起作用了!

Thanks for the fast replies, guys.感谢你们的快速回复,伙计们。 I'm a newcomer here, so I really mean it.我是新来的,所以我是认真的。

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

相关问题 此javascript代码在没有for循环的情况下可以正常运行,但是没有循环。任何帮助,我都尝试了一切 - This javascript code is running properly without the for loop but not with it.Any help, I have tried everything 无法加载JavaScript。 尝试了我能想到的一切 - JavaScript not being loaded. Tried everything I can think of 在javascript中过滤二维数组,我尝试了一切 - filtering a 2D array in javascript, i tried everything 测试 JavaScript 是否启用和/或正常运行 - Test if JavaScript is enabled and/or running correctly Javascript 分配有问题:数组和 object 迭代但我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything Javascript 分配有问题,我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment and I have no idea what the solution is, i have tried everything JavaScript 无法从 HTML 表单正确运行 - JavaScript not running correctly from HTML form $ injector:modulerr AngularJS,到目前为止已经尝试了所有方法 - $injector:modulerr AngularJS, tried everything so far Google Maps API 灰屏 - 尝试了一切 - Google Maps API grey screen - tried everything 使新的div可拖动,尝试了所有操作 - making a new div draggable, tried everything
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM