简体   繁体   English

JavaScript文件不仅运行HTML CSS

[英]JavaScript file isn't running only HTML CSS

Any type of javascript code I write and saves with no errors and when I open the html file which is linked to js and css (no jquery or other APIs). 我编写并保存的任何类型的javascript代码都没有错误,并且当我打开链接到js和css的html文件时(没有jquery或其他API)。 It works in all other online test environment (is it because i linked my js wrong). 它可以在所有其他在线测试环境中工作(是因为我将我的js链接错误)。 I did: 我做了:

<script src="javascript.js">

http://jsfiddle.net/tysvnr8q/ http://jsfiddle.net/tysvnr8q/

I read alot of answers else where but all of them never worked. 我在其他地方都读了很多答案,但所有答案都没有用。

Code in Brackets: 括号中的代码:

HTML 的HTML

<div id="circle"></div>

CSS 的CSS

#circle {
height: 100px;
width: 100px;
border-radius:100px;
background-color: red;
}

JS JS

document.getElementById("circle").onclick = function () {

       document.getElementById("circle").style.display = "none";
};

Wherever you put the script tag is where the script is run. 无论您将script标记放在何处,都是运行脚本的地方。 Eg If you put the script at the top of the body section or in the head, the script will run then, before loading the rest of your page. 例如,如果将脚本放在主体部分的顶部或头部,则脚本将在加载其余页面之前运行。 You can either place your code inside a window.onload() or 您可以将代码放入window.onload()或

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

Or you can place the script tag with the reference to the file (which I'm assuming is javascript.js and in the same folder) at the very bottom of the body section. 或者,您可以在正文部分的最底部将脚本标签与对文件的引用(假设是javascript.js并在同一文件夹中)一起使用。 Don't forget to close the tag. 不要忘记关闭标签。

Assuming the file javascript.js was actually in the same folder as your HTML file, then @nickRise has the correct answer. 假设javascript.js文件实际上与HTML文件位于同一文件夹中,则@nickRise具有正确的答案。

But I thought I'd clear up one bit of confusion from your question. 但是我想我会从您的问题中消除一点困惑。

You said in the title that your script doesn't run. 您在标题中说您的脚本没有运行。 Actually the script is running, but if you run that script before your DOM is built, then when your script executes 实际上该脚本正在运行,但是如果您构建DOM 之前运行了该脚本,那么在脚本执行时

document.getElementById("circle")

It does not find your element, because the element is not yet created! 它找不到您的元素,因为尚未创建该元素!

As @nickRise says, put the script at the bottom of your body, or have it run in the onload handler. 正如@nickRise所说,将脚本放在您的身体底部,或在onload处理程序中运行它。

It would be helpful when developing to look in the developer tools console. 在开发人员中查看开发人员工具控制台会很有帮助。 It might say something like undefined does not an onclick property (the actual error message might vary). 可能会说未定义之类的内容没有onclick属性(实际错误消息可能有所不同)。 Or, if by some chance the javascript.js file was in the wrong place, you might see an error saying that script was not found. 或者,如果javascript.js文件在错误的位置,您可能会看到一条错误消息,指出未找到脚本。

TL;DR the script was running, it just did not do what you thought. TL; DR脚本正在运行,但没有执行您所想的。

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

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