javascript - 内容未使用 jQuery Mobile 动态加载到 DIV 中

标签 javascript css jquery-mobile jquery

example没有具体说明问题,仅供引用。例如当我点击第 2 页时,div 中没有加载内容,我必须刷新页面才能看到第 2 页的内容。

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 问题。

您不应该将 document ready 与 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

关于javascript - 内容未使用 jQuery Mobile 动态加载到 DIV 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836856/

相关文章:

html - 伪元素的影子可以留在它的元素后面吗?

javascript - 更改函数 jquery 中的全局变量值

html - jQuery Mobile 和 PhoneGap 之间的区别

javascript - 如何只加载 yii2 中需要的 javascript 文件

javascript - 如何防止 jQuery prepend() 删除 HTML?

javascript - 更改选择字段的颜色也会更改与该选择字段关联的任何选项的颜色

html - 为什么 GMail 会忽略我在 HTML 电子邮件中的内联 <sup> 样式

javascript 追加方法 anchor 链接在 jquery mobile 中不起作用

javascript - 如何拥有同一 Javascript 模块的多个实例?

javascript - Angular-Ui Bootstrap Popup DatePicker 的 ngModelOptions 的时区属性