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

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

问题描述

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



更具体地说:


  1. 我通过输入加载我的主页url和一切都有效。

  2. 我通过点击主页中的链接加载下一页,
    DOM加载,但与页面中的页面相关联的脚本标题,
    不加载。

  3. 如果我刷新此页面,或者我直接在浏览器中输入此页面的URL
    (绕过页面转换),页面
    将脚本加载我知道jQuery手机试图用ajax来实现页面转换,但是我希望我可以把脚本保存在...中页眉页脚。我会讨厌将脚本放在< div data-role =page>

    解决方案

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



    有几个选项可以从如何解决这个问题:


    1. 您可以将页面特定的JS放在 data-role =page元素中,因此当jQuery Mobile通过AJAX载入页面时,它将被加载。

    2. 您可以将所有代码放在单个JS文件中,并将其包含在网站的每个页面中,只会在全页刷新上加载,但是这样,您网站的所有代码将可用的所有时间(在缩小和压缩之后,你真的需要有很多代码来使这个效率低下)。

    我是确定还有其他方法,但这里有一对我已经为我工作了的一对。


    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.

    To be more specific:

    1. I load my home page by typing in the url, and everything works.
    2. I load the next page by clicking on a link within home page, and the DOM loads, but scripts associated with the page in the page header, do not load.
    3. If I refresh this page, or I type the URL of this page in the browser directly (bypassing the page transitions), the page loads the scripts the way it should.

    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" >

    解决方案

    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. 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天全站免登陆