jQuery Mobile 未加载新页面脚本 [英] jQuery mobile not loading new page scripts

查看:23
本文介绍了jQuery Mobile 未加载新页面脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Django 中使用 jQuery Mobile:在页面转换期间,基本脚本似乎加载良好,但是从一个页面转到下一个页面,页眉下的新页面脚本不会在浏览器中加载.

Using jQuery Mobile with Django: During the page transitions, the base scripts seems to be loading fine, but going from one page to the next, new page's scripts under the header do not load up in the browser.

更具体地说:

  1. 我通过输入网址来加载我的主页,一切正常.
  2. 我通过单击主页中的链接加载下一页,然后DOM 加载,但与页面标题中的页面相关联的脚本,不要加载.
  3. 如果我刷新了这个页面,或者我输入了这个页面的 URL直接在浏览器中(绕过页面转换),页面以应有的方式加载脚本.

我知道 jquery mobile 尝试使用 ajax 实现页面转换,但我希望我可以将脚本保留在页眉/页脚中.我不想把脚本放在 <div data-role="page" >

I know that jquery mobile tries to implement page transitions using ajax, but I am hoping I can keep the scripts in header/footer. I would hate to have to put the scripts under <div data-role="page" >

推荐答案

这是因为 jQuery Mobile 仅加载 DOM 中第一个 data-role="page" 元素内的代码.

That is because jQuery Mobile loads only the code within the first data-role="page" element in the DOM.

您可以从几个选项中选择如何解决此问题:

There are a couple of options you can choose from on how to fix this:

  1. 您可以将特定于页面的 JS 放在 data-role="page" 元素中,以便在 jQuery Mobile 通过 AJAX 加载页面时加载它.
  2. 您可以将所有代码放在一个 JS 文件中,并将其包含在网站的每个页面中,它只会在整页刷新时加载,但这样您网站的所有代码将始终可用(在缩小和压缩之后,您确实必须编写大量代码才能使其效率低下).
  1. You can put your page-specific JS inside the data-role="page" element, so it will be loaded when jQuery Mobile loads the page via AJAX.
  2. You can put all of your code in a single JS file and include it in every page on the site, it will only be loaded on full-page refreshes but this way all the code for your site will be available all the time (after minification and compression you really have to have a lot of code to make this inefficient).

我确定还有其他方法,但这里有一些我为我工作的方法.

I'm sure there are other methods as well but here are a couple I've had work for me.

这篇关于jQuery Mobile 未加载新页面脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆