2013-02-12 80 views
0

我有一些自定义的jQuery事件我正在使用一个jQuery的移动网站。我在包含jquery-mobile之前包含了这个custom.js,因为我相信这是正确的行为。 jquery首先被包含在内。这些文件正在被包含在每个页面上。在jquery mobile中使用自定义jquery?

<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script src="/components/m/js/custom.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 

当我加载我的主索引页面时,没有使用custom.js的元素。这是当我点击一个页面深入我的问题。所以我点击一个链接来查看在custom.js中有一个事件的艺术家当jquery mobile做它的事情时,我的点击事件不起作用。

但是,如果我重新加载页面,自定义点击事件将工作得很好。

我错过了什么?我应该使用.live(“点击”,而不是只。点击?

+0

您是否已尝试使用live('click')?对我来说,这将是最容易找到的最简单的方法。 – Aeolun 2013-02-12 02:13:17

+0

我注意到live('click')在触发时有点作用,但是我的ajax调用不起作用。但是,如果我刷新页面,它会工作得很好。所以宝贝步骤。 – Mike 2013-02-12 02:14:16

+0

这个更深层的页面,无论如何,一个多页(包含多个'div'标签,包含'data-role =“page”')jQM文件? – peterm 2013-02-12 05:55:28

回答

0

与Gajotres(通过电子邮件从上述评论)沟通后,我们来到了以下结论。

我的自定义JS文件实际上不得不来的JQM文件后

<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
<script src="/components/m/js/custom-alt.js"></script> 

而且,这是我的自定义的js文件的样子:

$(document).bind("pageinit", function() { 
    var btnAddComment = $('#btn-addcomment'); 

    // register functions with jqm framework 
    $(btnAddComment).bind('touchstart mousedown', function(e){ 
     e.preventDefault(); 
     clickCommentAdd(); 
    }); 

    function clickCommentAdd() { 
     $("#form-comment").toggle(); 
     return false; 
    } 
}); 

以前,我是用“mobileint”而不是“pageint”和引用自Gajotres:

原因是,pageinit事件只在加载jQuery Mobile js文件时起作用。我也看到你甚至尝试过使用mobileinit。那么 该事件应该在jQuery Mobile js文件之前使用(迄今为止很好),但是您可以使用它来绑定和单击事件,因为您需要加载jQM js文件来初始化页面元素。