最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem adding click event handler with JQuery Mobile in Android Browser - Stack Overflow

programmeradmin2浏览0评论

I am having a strange problem with the android browser. Hopefully this is something I am doing fundamentally wrong and not a JQuery bug. I have tried the following code in Firefox and Chrome which work fine. However when I try it in the Android browser, clicking on the Get Tweets button does not work. It seems to be an issue with JQuery Mobile because when I add the event to a normal non JQuery Mobile object it binds correctly and the console.log fires. I have tried with both alpha and beta versions of JQuery Mobile with no luck.

<!DOCTYPE html> 
<html>
    <head>
        <style type="text/css" rel="stylesheet" >
            .chkTag { color: black; }
            /*.chkFeed { color: black; }*/
        </style>
        <link rel="stylesheet" href="twitter/style/jquery.mobile-1.0a4.1.min.css"     />        
        <script type="text/javascript" src="twitter/js/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="twitter/js/jquery.mobile-1.0a4.1.js">       
</script>
    <script type="text/javascript">
    $(document).ready(function() {            
        $(".tweetjack").click(function() {
            console.log('test');
        });
        console.log('page loaded');
    });
    </script>
    <style type="text/css">
        .profile img { width: 48px; height: 48px; display: inline; float: left; padding-right: 10px; }
        /*.ui-collapsible-contain { max-height: 950px; min-height: 950px; }*/
    </style>
</head>
<body>
    <div id="page" data-role="page" data-theme="d">
        <div id="header" data-role="header">
            <h1>Twitter App</h1>
        </div>
        <div data-role="content">
            <div id="1" class="tweetjack" data-role="collapsible" data-collapsed="true"><h3>Get Tweets</h3><p id="text1"></p><div data-role="collapsible" data-collapsed="true"><h3 date-theme="a">Filter</h3></div></div>
        </div>
        <div id="footer" data-role="footer">
            <a class="tweetjack"><h2>BT</h2></a>
        </div>
    </div>
</body>
</html>

This script also fails in android browser only.

$('#page').live('pagecreate',function(event){
      console.log('page loaded');
      $(".tweetjack").click(function() {
            console.log('test');
        });
    });

I am having a strange problem with the android browser. Hopefully this is something I am doing fundamentally wrong and not a JQuery bug. I have tried the following code in Firefox and Chrome which work fine. However when I try it in the Android browser, clicking on the Get Tweets button does not work. It seems to be an issue with JQuery Mobile because when I add the event to a normal non JQuery Mobile object it binds correctly and the console.log fires. I have tried with both alpha and beta versions of JQuery Mobile with no luck.

<!DOCTYPE html> 
<html>
    <head>
        <style type="text/css" rel="stylesheet" >
            .chkTag { color: black; }
            /*.chkFeed { color: black; }*/
        </style>
        <link rel="stylesheet" href="twitter/style/jquery.mobile-1.0a4.1.min.css"     />        
        <script type="text/javascript" src="twitter/js/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="twitter/js/jquery.mobile-1.0a4.1.js">       
</script>
    <script type="text/javascript">
    $(document).ready(function() {            
        $(".tweetjack").click(function() {
            console.log('test');
        });
        console.log('page loaded');
    });
    </script>
    <style type="text/css">
        .profile img { width: 48px; height: 48px; display: inline; float: left; padding-right: 10px; }
        /*.ui-collapsible-contain { max-height: 950px; min-height: 950px; }*/
    </style>
</head>
<body>
    <div id="page" data-role="page" data-theme="d">
        <div id="header" data-role="header">
            <h1>Twitter App</h1>
        </div>
        <div data-role="content">
            <div id="1" class="tweetjack" data-role="collapsible" data-collapsed="true"><h3>Get Tweets</h3><p id="text1"></p><div data-role="collapsible" data-collapsed="true"><h3 date-theme="a">Filter</h3></div></div>
        </div>
        <div id="footer" data-role="footer">
            <a class="tweetjack"><h2>BT</h2></a>
        </div>
    </div>
</body>
</html>

This script also fails in android browser only.

$('#page').live('pagecreate',function(event){
      console.log('page loaded');
      $(".tweetjack").click(function() {
            console.log('test');
        });
    });
Share Improve this question edited Jul 5, 2011 at 17:00 Caimen asked Jul 5, 2011 at 15:44 CaimenCaimen 2,6192 gold badges26 silver badges43 bronze badges 4
  • 1 Try using the 'pagecreate' event rather than $(document).ready(): jquerymobile./demos/1.0b1/#/demos/1.0b1/docs/api/events.html – Ahmed Nuaman Commented Jul 5, 2011 at 16:10
  • I replaced .ready() with this and again the same issue. Click event handler works fine in all browsers except android mobile. – Caimen Commented Jul 5, 2011 at 16:56
  • How are you viewing the console? – Ahmed Nuaman Commented Jul 5, 2011 at 17:01
  • In firefox I use firebug, in Android I use Dalvik Debug monitor. The 'page loaded' log is seen in ddms, but the click log is not. Both are seen in firebug. – Caimen Commented Jul 5, 2011 at 17:04
Add a ment  | 

2 Answers 2

Reset to default 6

So it seems I was simply doing it the old fashioned JQuery way when there is actually a JQuery Mobile way of doing these things now. Here is the correct code.

$('#page').live('pagecreate',function(event){
  console.log('page loaded');
  $(".tweetjack").live('tap',function(event) {
      console.log('test');
  });
});

JQM seems to intercept clicks, and set the href to '#', which can cause some havoc if you're trying to intercept them. This worked for me, to process the link before JQM gets to it:

$(document).bind("pageinit", function() {
   $("a").bind("click", function(e){  fix_mobile_links(this, e)  });
}
发布评论

评论列表(0)

  1. 暂无评论