-2

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

<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时甚至不工作。

我从来没有达到 alert("end") 语句,也从来没有达到 alert("foo") 语句。

如何使用 $(docuemnt).ready 编写一些代码。

4

2 回答 2

2

你有没有尝试过?

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

-编辑-

在 window.load 中执行的函数内准备好的文档有点多余..,警报“结束”也在 document.ready 之外

于 2013-07-28T18:03:10.770 回答
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/

于 2013-07-28T18:52:53.237 回答