为什么ID类我在我的单独的.JS页面不工作 [英] Why Is The ID class i styled on my seperate .JS page not working
问题描述
所以这就是我在单独的页面上使用JavaScript时所做的:为什么它不起作用(我确定源页面名称是yellow.js,我只是不知道如何分隔这里的javascript文件)
So this is what i did when i used the put the JavaScript on a separate page: Why is it not working?????(And i made sure the source page name is yellow.js and i just dont know how to seperate the javascript file on here)
<!DOCTYPE html>
<html>
<head>
<script src="yellow.js"></script>
</head>
<body>
<h1 id="box" onload="change()">NBA Legends</h1>
</body>
</html>
function change() {
var box = document.getElementById("box");
boxStyle = box.style;
boxStyle.color = 'red';
}
推荐答案
window.onload = function change()
{
var box = document.getElementById("box");
var boxStyle = box.style;
boxStyle.color = 'red';
}
<!DOCTYPE html>
<html>
<head>
<script src="yellow.js"></script>
</head>
<body>
<h1 id="box" onload="change()">NBA Legends</h1>
</body>
</html>
以下是
window.onload 之间的区别 - 在所有DOM,JS文件,图像,iframe,扩展和其他完全加载。这等于$(window).load(function(){});
window.onload - it is called after all DOM, JS files, Images, Iframes, Extensions and others completely loaded. This is equal to $(window).load(function() {});
onload = - 它被称为一次DOM加载。这是等于$(document).ready(function(){});
onload="" - It is called once DOM loaded. This is equal to $(document).ready(function() {});
编写干净的代码总是很好,我总是喜欢使用window.onload ,而不是在元素上使用onload事件。
It is always good to write clean code and I always prefer to use window.onload, instead of using onload event on element.
这篇关于为什么ID类我在我的单独的.JS页面不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!