为了更好地组织我们的 JQuery Mobile PhoneGap 应用程序,我希望能够在新页面上使用标签,并在页面被引入时通过 changePage 或其他方式引入 JS。
到目前为止,我为此所做的努力并没有产生我想要的结果。除非我在 index.html 上包含所有 JS 文件,然后在 PageShow 事件处理程序中编写条件逻辑,这不是一个好的解决方案。
有什么想法吗?
最佳答案
要向使用 jQuery Mobile(通过 AJAX)加载的页面引入新的 JS,您必须在 data-role="page"
或(data-role ="dialog"
) 每个页面的元素,因为 jQuery Mobile 在 AJAX 页面加载期间不会处理这些元素之外的任何内容。
或者,您可以使用 JavaScript 在每个页面转换的“pageshow”事件上动态创建新的 script
标记。像这样:
$(document).on('pageshow', 'div[data-role*="page"],div[data-role*="dialog"]', function () {
(function () {
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
script.src = '/path/to/new/include.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
})();
});
关于javascript - JQuery Mobile Change Page 不加载 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540219/