Document.onload无法在JavaScript中运行? [英] Document.onload not working in JavaScript?

查看:95
本文介绍了Document.onload无法在JavaScript中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在head标签内写了 document.onload 代码..但它似乎不起作用。
这是我的代码..

I write document.onload code inside my head tag.. but it doesn't seems to work. here is my code..

<head>
<script>
    document.onload = function () {
        alert("Window Loaded...!!");
    }
</script>
</head>

但如果我用替换window.onload 它完美地运作!!
有什么问题?我做错了吗?

But if i replace with window.onload it perfectly works!! What is the problem? Am I doing something wrong ??

推荐答案

据我所知,你最接近你的方法的是:

As far as I'm aware, the closest you can come to your method is:

document.addEventListener('DOMContentLoaded', function () {
   /* your logic here */
});

您遇到的问题是文件可能不会为特定浏览器提供 onload 的方法。幸运的是!在大多数情况下,窗口都可以。 :)试试你的JavaScript调用。

The problem you have is document may not have a method of onload for a particular browser. Luckily! The window does in most cases. :) Give that a try for your JavaScript invocation.

这篇关于Document.onload无法在JavaScript中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆