1

我一直在试图解决这个问题,但仍然卡住了。所以我在 iOS 和 JQueryMobile 上使用 PhoneGap。我试图在单击按钮时显示警报。

<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 模拟器时,它没有显示任何内容。

4

1 回答 1

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 ready 中的所有代码都不会再次被调用。因此,我在上面放置的第一段代码仅适用于您的 JQM 应用程序的第一页。无论您的按钮位于哪个页面,第二个都可以使用。如果我可以为您进一步澄清这一点,请告诉我。

于 2012-04-25T10:11:27.130 回答