【问题标题】:Unexpected page event execution order意外页面事件执行顺序
【发布时间】:2012-06-27 09:03:35
【问题描述】:

我对以下的执行顺序感到困惑:

$('#home').live('pageinit',function(){
    $('#test').hide();

    $(function() { 
        alert('test1');
    });

    $('#button').click(function(event) {
        event.preventDefault();
    });
});

$('#home').live('pageshow',function(){
    alert('test3');
});

顺序如下:

  1. pageinit 被解雇了
  2. $('#test').hide() 被执行
  3. $('#button').click()... 被执行
  4. pageshowalert('test3')走开
  5. alert('test1') 被解雇

为什么alert('test1') 不作为数字 3 执行?

谢谢

【问题讨论】:

    标签: jquery-mobile


    【解决方案1】:

    我不确定代码中是否有任何拼写错误,但它应该会在 Inspect Element 或 Developer Console 中显示错误。尝试像这样封装函数:

    (function() { 
       alert('test1');
    })();
    

    查看fiddle 的完整源代码

    【讨论】:

    • 是的,那些是错别字(现已修复)。实际代码是正确的,但有一个有趣的序列问题。
    • 你检查过小提琴吗,小提琴中的序列可以按照你的意愿工作
    • 如你所说!那么使用$(function(){}); 有什么问题呢? “延迟”执行的机制是什么?
    • 找到答案:stackoverflow.com/questions/3908724/… 这是$(document).ready(function () { })的简写
    猜你喜欢
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多