php - Jquery 手机 : New Ajax Loaded Page not Loading new JS

标签 php jquery jquery-mobile

这是我当前的设置。

我有两个页面在 jquery 移动框架上运行。

  • index.php
  • 文章.php

在打扰 header 中,我有一个名为 ratings.js 的 js 文件。

这是我的 js ratings.js:

$(document).ready(function(){
    $("#rating_1").click(function () {
        $("#rating_2").css('backgroundPosition', '0px 0px');
        $(this).css('backgroundPosition', '-45px 0px');
    }); 
}); 

当我加载 index.php 然后转到 article.php 时,我对 ratings.js 的触发器都不起作用。

如果我添加 rel="external"data-ajax="false" 我的触发器工作。 然而,我失去了让我的装载机轮子出现的能力。

有什么建议吗?

谢谢!

最佳答案

Important: Use $(document).bind('pageinit'), not $(document).ready()

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.

尝试:

$(document).bind('pageinit', function() {
    $("#rating_1").click(function() {
        $("#rating_2").css('backgroundPosition', '0px 0px');
        $(this).css('backgroundPosition', '-45px 0px');
    }); 
});

关于php - Jquery 手机 : New Ajax Loaded Page not Loading new JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11068444/

相关文章:

javascript - Jquery Mobile 和 Cordova 事件 Pageinit 绑定(bind)不起作用

javascript - jQuery 以错误的顺序执行代码部分

Jquery 验证器 - 仅验证父级可见的元素

单选按钮的 jQuery 验证插件错误消息位置

php - 在 PHP 函数中避免 SQL 注入(inject)

javascript - 页脚中的导航栏项目

javascript - 正则表达式仅验证数字字符串和长度

PHP:从对象中获取数据

php - 我可以使用哪些编程语言来编写 Bonjour 软件?

php - 使用企业代理的后备解决方案嵌入 YouTube 视频