我有下面的代碼的問題:的document.ready不可達
<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>
當我做了一些workarround,我沒發現有什麼不對的代碼。
上面的代碼被保存爲php,但是當保存爲html時,甚至不會wor inking。
我從來沒有達到alert(「end」)語句,也從來沒有達到alert(「foo」)語句。
我該如何用$(docuemnt).ready編寫一些代碼。
你需要把'$(文件).ready'功能'firstload外()'函數。如果你想等待所有的元素加載,你可以使用'$(window).load()'函數。 –
爲什麼使用額外的事件偵聽器而不只是$(document).ready? – Iansen