【发布时间】:2011-01-28 21:36:46
【问题描述】:
我目前正在使用 jQuery Mobile“pagebeforecreate”页面初始化事件将内容加载到我的 html 页面中。此处讨论 jQuery Mobile 事件:http://jquerymobile.com/demos/1.0a2/#docs/api/events.html。在内容同步加载并附加到正文后,jQuery Mobile 会格式化页面。
这是我在支持 jQuery Mobile 的浏览器中工作的代码:
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile Test</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
$('body').live('pagebeforecreate',function(event){
var ajaxContent = "";
$.ajax({
async: false,
type: "GET",
url: "test.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('item').each(function()
{
ajaxContent += '<li><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a></li>';
});
//alert(ajaxContent);
$('#menu').append(ajaxContent);
}
});
});
</script>
<script src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>
</head>
<body>
<noscript>You must have JavaScript enabled to view the content on this website.</noscript>
<div data-role="page">
<div data-role="header">
<h1>jQuery Mobile</h1>
</div>
<div data-role="content">
<ul data-role="listview" id="menu">
<li><a href="#">Static</a></li>
</ul>
</div>
</div>
</body>
</html>
这里是 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<item>
<title>Awesome Link from XML 1</title>
<link>http://www.google.com</link>
</item>
<item>
<title>Awesome Link from XML 2</title>
<link>http://www.gmail.com</link>
</item>
</menu>
如何检测浏览器是否支持“pagebeforecreate”jQuery Mobile 事件?或者有没有办法检测这个事件是否被执行?如果此事件从未执行,我需要使用另一个函数加载 XML。即使 IE 8 不支持 jQuery Mobile,我仍然希望它能够处理 XML 文件并显示链接。
【问题讨论】: