我有一个MVC应用程序,它使用PHP检查查询字符串中的内容,并通过呈现页眉、内容和页脚来加载适当的页面(例如,?category=1加载呈现类别视图的CategoryController )。
我还有一个向DOM中的元素添加jQuery功能的custom.js。因为动态内容是由PHP控制的,所以jQuery功能只有在页面首先加载时才起作用。遍历应用程序不会刷新页面,因此新包含的元素不会添加到jQuery对象中。
有什么办法解决这个问题吗?
发布于 2013-01-21 23:44:05
首先,不要在jQuery移动端使用$(function()或$(document).ready(),原因很简单,因为jQM不是这样构建的。相反,您应该使用这里提到的页面事件:https://stackoverflow.com/a/14010308/1848600或mobileinit事件,如下所示:
$(document).on("pageinit", function () {
});或者mobileinit,如果你想让它在应用程序执行时只执行一次:
$(document).on("mobileinit", function () {
});原因在顶部链接中描述。此外,您还可以在jQM官方文档中找到有关这方面的更多信息:http://jquerymobile.com/test/docs/api/events.html
现在,如果你想让jQM重新定义你的页面样式,你应该使用.trigger('pagecreate')函数。
比方说,您有一个id为索引的jQM页面,这是您生成的布局。
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
<a href="#second" class="ui-btn-right">Next</a>
</div>
<div data-role="content">
<a href="#" data-role="button" id="test-button">Test button</a>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div> 要强制jQM对其进行样式设置,您应该使用类似以下内容:
$('#index').live('pagebeforeshow',function(e,data){
$('#index').trigger('pagecreate');
});或者,如果您希望将其应用于每个jQM页面,则可以像这样使用它:
$('[data-role="page"]').live('pagebeforeshow',function(e,data){
$(this).trigger('pagecreate');
});https://stackoverflow.com/questions/14440949
复制相似问题