使用jQuery mobile无法动态加载到DIV中的内容
问题描述:
此example没有显示确切的问题,只是有一个想法。例如,当我点击页面2时,内容不会加载到div中,我必须刷新页面才能看到页面2的内容。使用jQuery mobile无法动态加载到DIV中的内容
P.S:相同的代码在其他页面中重复。
代码:
$(document).ready(function() {
$('.content-primary').html("Content of page 1"); // This line just in this example
//$(".content-primary").load("content-of-page.php"); // but I used this code on all my pages
});
答
这是一个常见的jQuery Mobile的问题。
您不应该使用jQM准备好文档,而应该使用jQM提供的页面事件。您的问题是文档已准备好谁可以,并且通常在页面加载到DOM之前触发。这就是为什么刷新有帮助,在这一点上已经加载页面。
我给你做一个工作示例:http://jsfiddle.net/Gajotres/8hKe2/
$(document).on('pagebeforeshow', '#foo', function(){
alert('This is foo');
});
$(document).on('pagebeforeshow', '#bar', function(){
alert('This is bar');
});
基本上每个页面事件将触发JavaScript代码只是针对该网页。如果你希望你的代码只执行一次,pageinit事件应该被用来代替pagebeforeshow,像这样:
$(document).on('pageinit', '#foo', function(){
alert('This is foo');
});
$(document).on('pageinit', '#bar', function(){
alert('This is bar');
});
如果您想了解更多的看看我的另一篇文章/回答:https://stackoverflow.com/a/14469041/1848600
你意思是这样的http://jsfiddle.net/gCsDz/6/ – Morpheus 2013-04-05 14:45:14
哦对不起,当我更新JSFIDDLE,我得到这个错误。 – Mils 2013-04-05 14:47:51