jquery mobile + phonegap onclick

时间:2012-04-25 04:58:14

标签: jquery-mobile cordova

我一直试图解决这个问题,但仍然陷入困境。 所以我正在使用PhoneGap for iOS和JQueryMobile。 我试图在点击按钮时显示警告。

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
<script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
        </script>
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js">
        </script>

我有 这个

<a href="#" id="button" data-role="button" data-theme="b"> Login </a> $(document).ready("#button").click(function() { alert('button clicked'); });

当我尝试在chrome中启动它时,这很好用。 但是,当我使用iphone模拟器时,它并没有显示任何内容。

1 个答案:

答案 0 :(得分:7)

对于普通的Web应用程序,您可以使用dom ready。

$(function(){ // <-- this is a shortcut for $(document).ready(function(){ ... });
    $('#button').click(function(){
        alert('button clicked');
    });
});

然而,在JQM应用程序中,绑定到'pageinit'更有用,即

$(document).on('pageinit','[data-role=page]',function(){
    $('#button').click(function(){
        alert('button clicked');
    });
});

绑定到pageinit效果更好,因为JQM将新页面插入到与第一页相同的dom中。因此,dom准备好的所有代码都不会被再次调用。所以我上面的第一段代码只适用于JQM应用程序中的第一页。无论您的按钮在哪个页面,第二个都可以工作。如果我能为您进一步澄清,请告诉我。