简体   繁体   English

为所有内容添加一个外部js文件?

[英]Add one external js file for everything?

I have an HTML page that I want to perform several lines of js code on. 我有一个HTML页面,我想在上面执行几行js代码。 For now I'm just trying one thing: 现在,我只是尝试一件事:

document.getElementById("title").style.color = "blue";

This is in jsPage.js the within the head of my html page I have 这是在jsPage.js中,我的html页面的头部

<script src="jsPage.js"></script>

I thought this was all that was necessary. 我认为这就是必要的一切。 But my title is staying red (this is what it is set to in the css doc attached to html page) instead of turning blue. 但是我的标题一直保持红色(这是在html页面附带的CSS文档中设置的名称),而不是变成蓝色。 Am I missing some standard declaration or something in the js file? 我是否在js文件中缺少一些标准声明或其他内容?

Assuming that document.getElementById("title").style.color = "blue"; 假设document.getElementById("title").style.color = "blue"; is in jsCalc.js , you're most likely not changing the text color after the window loads. jsCalc.js ,您很可能在加载窗口后不更改文本颜色。

The content of jsCalc.js should be jsCalc.js的内容应为

window.onload = function() {
    document.getElementById("title").style.color = "blue";
};

as you cannot change DOM elements before they have loaded. 因为您无法在DOM元素加载之前更改它们。

Likely, since you are loading the js file in the head of your document, the javascript is running before the document is loaded and the title element exists. 可能是因为您要在文档的头部加载js文件,所以javascript在文档加载和title元素存在之前就已经在运行。

There are a couple of ways to deal with it, but the simplest is to move your script declaration to the bottom of your file, instead of the head , so that the script is loaded after the document elements it references. 有两种处理方法,但是最简单的方法是将script声明移到文件的底部而不是head ,以便在脚本引用的文档元素之后加载脚本。

Vince's answer is another way of ensuring the javascript is run after the elements in question are loaded. 文斯的答案是确保在加载有问题的元素之后运行javascript的另一种方法。

Since the <script> tag is in the head it is getting executed before the body has been rendered, so element "title" does not exist. 由于<script>标记位于头部,因此在渲染正文之前就已执行该标记,因此元素“ title”不存在。

There are better ways to do this, but putting the <script> tag just before </body> should work. 有更好的方法可以执行此操作,但是可以将<script>标记放在</body>之前。

As Mentioned by Vince: You should use window.onload so the the code execution will wait till the window is loaded and it makes sure that the DOM is present. 正如Vince所提到的:您应该使用window.onload,以便代码执行将一直等到加载窗口并确保DOM存在为止。

But as an add on it is always a good practice to load javascript in body. 但作为补充,将javascript载入体内始终是一个好习惯。 Else your page loading will be stuck till that file is fetched. 否则,您的页面加载将被阻塞,直到提取该文件为止。

In some cases js is getting loaded from some other server and its taking time, DOM will also not render. 在某些情况下,js是从其他服务器加载的,并且花费时间,因此DOM也不会呈现。

And, you can keep the scripts in the head itself by using "defer" atrribute on it. 并且,您可以通过在脚本上使用“ defer”属性来将脚本保留在文件头中。 This will serve the same purpose. 这将达到相同的目的。 "Defer" makes sure its loaded after everything is done. “延迟”确保在完成所有操作后将其加载。

Something like: 就像是:

<script type="text/javascript" scr="page.js" defer></script>

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

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