简体   繁体   English

jQuery Mobile创建新页面div后如何执行自定义js?

[英]How to execute custom js after jQuery Mobile has created a new page div?

So I am using Django 1.3 and jQuery Mobile for a webapp.因此,我将 Django 1.3 和 jQuery Mobile 用于 Web 应用程序。 When trying to create new functionality or override some of jQM's functionality I don't seem to be able to get it to excute some code on page creation.在尝试创建新功能或覆盖 jQM 的某些功能时,我似乎无法让它在页面创建时执行某些代码。 I am still hackish at js, but it seems to be a bigger problem than myself How to execute JavaScript after a page is transitioned with jQuery Mobile我仍然对 js 很敏感,但这似乎是比我自己更大的问题如何在使用 jQuery Mobile 转换页面后执行 JavaScript

I end up putting js snippets on the page itself which doesn't seem the correct way to handle they work sometimes and sometimes not.我最终将 js 片段放在页面本身上,这似乎不是处理它们有时工作有时不工作的正确方法。 I have tried the same commands in the script console of Chrome and the selector and commands seem to work fine.我在 Chrome 的脚本控制台中尝试了相同的命令,并且选择器和命令似乎工作正常。

examples:例子:

Hiding the numeric inputs on on sliders I end up putting this script tag in the template itself, I know this is bad form, but am unsure how to get it to work otherwise:隐藏滑块上的数字输入我最终将这个脚本标签放在模板本身中,我知道这是不好的形式,但我不确定如何让它工作:

<script>
    $('#form_div > input').hide();
</script>

Trying to do a similar snippet:尝试做一个类似的片段:

<script>
        console.log("Focus snippet!");
        $('.ui-input-text').blur(function(){
            console.log("focus was changed!");
        });
</script>

yields no results, except the initial console.log, but I can execute through the script console and it works fine.除了初始的console.log 之外没有产生任何结果,但我可以通过脚本控制台执行并且它工作正常。

I saw in this several other posts, but none have seemed to answer the question clearly and I am unsure how how to make this work the right way.我在其他几篇文章中看到过,但似乎都没有清楚地回答这个问题,我不确定如何以正确的方式进行这项工作。

This seemed the closest suggestion, but I was unable to make it work: Jquery mobile: how to execute custom jquery code in page这似乎是最接近的建议,但我无法使其工作: Jquery mobile: how to execute custom jquery code in page

$(“body”).delegate(“div[data-role*='page']“, “pageshow”, function(){

// Your code here. It is good to check to not run unnecessary code

});

Any suggestions would be greatly appreciated.任何建议将不胜感激。

Look at the documentation here: http://jquerymobile.com/demos/1.0a4.1/#docs/api/events.html在此处查看文档: http://jquerymobile.com/demos/1.0a4.1/#docs/api/events.html

$('div').live('pageshow',function(event, ui){
  alert('This page was just hidden: '+ ui.prevPage);
});

$('div').live('pagehide',function(event, ui){
  alert('This page was just shown: '+ ui.nextPage);
});

One small note is that all the javascript executed on any page must go in the base page (like: index.html).一个小提示是在任何页面上执行的所有 javascript 必须在基本页面中 go (如:index.html)。 If you add javascript for page2.html in page2.html it will not be executed.如果在 page2.html 中为 page2.html 添加 javascript 则不会执行。 if you add the javascript for page2.html in index.html it will be executed.如果在 index.html 中为 page2.html 添加 javascript,它将被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM