document.ready无法访问

时间:2013-07-28 18:01:35

标签: jquery

我在以下代码中遇到问题:

<html>
<head>
    <link href='http://fonts.googleapis.com/css?family=PT+Sans:400italic' rel='stylesheet' type='text/css'>  
    <link href='http://fonts.googleapis.com/css?family=PT+Sans' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=PT+Sans:700' rel='stylesheet' type='text/css'>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
</head>
<body>
<input type = "button" value = "test" />
<script>
function firstload() {
    alert("start");
    $(document).ready(function () {
        alert("foo");
    });
    alert("end");
}
addEventListener('load', function () {
    'use strict';
    setTimeout(firstload, 0);
}, false);
</script>
</body>
</html>

当我做了一些工作时,我没有发现代码有什么问题。

上面的代码保存为php,但是当保存为html时甚至不会出现问题。

我从未达到警告(“结束”)声明,也从未达到警告(“foo”)声明。

如何用$(docuemnt).ready。

编写一些代码

2 个答案:

答案 0 :(得分:2)

你尝试过吗?

function firstload() {
    alert("end");
}
$(function(){ /* or $(document).ready(){ */ /* if you prefere */
    firstload();
});

- 编辑 -

在窗口中执行的函数内部准备好的文件。加载它有点多余..,警报“结束”也在文档之外。已经

答案 1 :(得分:1)

就个人而言,我从未使用过'addEventListener',尤其是在执行onready函数时。

jquery的最佳实践是使用类似的东西:

$(function(){
   //insert code here
});

//insert other functions out here

以下是.ready:

的使用方法
$(document).ready(function() {
   //insert code here
});

//insert other functions out here

我无法告诉你为什么你的代码不起作用,但我肯定知道这会。

以下是jquery网站的链接:http://api.jquery.com/ready/