【发布时间】:2013-02-06 17:02:58
【问题描述】:
我有一个index.html 文件,语法如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<a href="occassion.html">
<img class="frametoicon" src="img/occasion.png" />
</a>
</body>
</html>
然后我有occassion.html 页面,其中有body onload 功能如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Occassion</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
function loaded() {
alert("hii");
}
</script>
</head>
<body onLoad="loaded()">
</body>
</html>
但是onload 函数没有启动....如果我尝试刷新页面 (occassion.html) 然后onload 函数被启动...为什么从index.html 导航时它不工作?
如果我删除 jQuery 文件,那么它会按预期工作....我缺少什么要添加的内容?
这也不行
$(document).ready(function () {
loaded();
});
编辑
我加了
<script>
$(document).bind("pagechange", function (event, data) {
console.log(data);
var toPage = data.toPage[0].id;
alert(toPage);
if (toPage == "page2")
alert("hello");
});
</script>
并将以下内容放入 occassion.html
<div data-role="page" id="page2">
hello
</div>
令我惊讶的是,甚至 alert(hello) 都没有触发,而且 hello 也没有显示,并且 alert(topage) 为空..
怎么样?缺少什么
【问题讨论】:
-
你为什么用
onLoad而不是$(document).ready? -
不是答案,但正确的拼写是场合......
-
您正在使用 jQuery - 不要使用
onload,而是依赖脚本中的$(function(){ .. })块。 -
如果您在使用 jQuery 时仍在使用内联 JavaScript,那么您真的需要重新考虑整个方法。
标签: javascript jquery html jquery-mobile