2017-10-11 188 views
0

我開發了被指定爲混合Android應用程序(phonegap)的html + onsenui + jQuery應用程序,但我面臨的問題是佈局和點擊/觸摸事件不起作用。jquery點擊/觸摸事件不適用於通過電話捆綁的混合應用程序

下面是代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css"> 
    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css"> 
    <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script> 
    <script src="https://unpkg.com/jquery/dist/jquery.min.js"></script> 

    <script> 
    function showModal() { 
    var modal = document.querySelector('ons-modal'); 
    modal.show(); 
    var i = 1 
    var myTimer = setInterval(function(){ 
     $("#message").html(i); 
     if (i <= 30){ 
     i += 1; 
     } 
    }, 1000); 


    setTimeout(function() { 
    modal.hide(); 
    clearInterval(myTimer); 
    $("#message").html(""); 
    ons.notification.toast({message: "Now you are free to open/close again.", timeout: 2000}); 
    }, 30000); 
} 


$(function(){ 
$('#btn-outdoor-open').on("click touchend", function() { 

     $.ajax({ 
      url: 'http://localhost:5000/outer_door/2JkHmmbv4gA6mXLF', 
      type: 'POST', 
      success: function (result) { 
       if (result["response"] == "Not Authorized"){ 
        ons.notification.toast({message: result["response"], timeout: 1000}); 
       } 
       else{ 
        ons.notification.toast({message: result["response"], timeout: 1000}); 
        ons.notification.toast({message: 'Switching on relay for outer doors..', timeout: 3000}); 
        showModal(); 
       } 
      }, 
      error: function(xhr, textStatus, errorThrown){ 
       ons.notification.toast({message: 'Failed', timeout: 3000}); 
      } 
     }); 



}); 



$('#btn-indoor-open').on("click touchend", function() { 


     $.ajax({ 
      url: 'http://localhost:5000/inner_door/2JkHmmbv4gA6mXLF', 
      type: 'POST', 
      success: function (result) { 
       if (result["response"] == "Not Authorized"){ 
        ons.notification.toast({message: result["response"], timeout: 1000}); 
       } 
       else{ 
       ons.notification.toast({message: result["response"], timeout: 1000}); 
       ons.notification.toast({message: 'Switching on relay for inner doors..', timeout: 3000}); 
       showModal(); 
       } 

      }, 
      error: function(xhr, textStatus, errorThrown){ 
       ons.notification.toast({message: 'Failed', timeout: 3000}); 
      } 
     }); 


}); 

$('#btn-both-open').on("click touchend", function() { 


     $.ajax({ 
      url: 'http://localhost:5000/inner_door/2JkHmmbv4gA6mXLF', 
      type: 'POST', 
      success: function (result) { 
       if (result["response"] == "Not Authorized"){ 
        ons.notification.toast({message: result["response"], timeout: 1000}); 
       } 
       else{ 
        ons.notification.toast({message: result["response"], timeout: 1000}); 
        ons.notification.toast({message: 'Switching on relay for inner doors..', timeout: 3000}); 
        ons.notification.toast({message: '5 seconds delay before another switch', timeout: 3000}); 
       } 
      }, 
      error: function(xhr, textStatus, errorThrown){ 
       ons.notification.toast({message: 'Failed', timeout: 3000}) 
      } 
     }); 



     setTimeout(function(){ 

      $.ajax({ 
       url: 'http://localhost:5000/outer_door/2JkHmmbv4gA6mXLF', 
       type: 'POST', 
       success: function (result) { 
        if (result["response"] == "Not Authorized"){ 
         ons.notification.toast({message: result["response"], timeout: 1000}); 
        } 
        else{ 
         ons.notification.toast({message: result["response"], timeout: 1000}); 
         ons.notification.toast({message: 'Switching on relay for outer doors..', timeout: 3000}); 
         showModal(); 
        } 
       }, 
       error: function(xhr, textStatus, errorThrown){ 
        ons.notification.toast({message: 'Failed', timeout: 3000}); 
       } 
      }); 

     }, 5000); 


}); 

}); 

</script> 

</head> 



<body style="font-family: monospace;"> 
<ons-page> 
    <div id="btn-outdoor-open" style="height: 33.33%; text-align: center; padding-top: 17%; background-color: #0F2043; color: white;"> 
     <h2>Outer</h2> 
     <ons-ripple></ons-ripple> 
    </div> 

    <div id="btn-indoor-open" style="height: 33.33%; text-align: center; padding-top: 17%; background-color: #79CEDC;"> 
     <h2>Inner</h2> 
     <ons-ripple></ons-ripple> 
    </div> 

    <div id="btn-both-open" style="height: 33.34%; text-align: center; padding-top: 17%; background-color: #D5A458;"> 
     <h2>Both</h2> 
     <ons-ripple></ons-ripple> 
    </div> 

</ons-page> 

    <ons-modal direction="up"> 
     <div style="text-align: center"> 
      <p> 
       Operation takes 30 seconds to finish<br><br> 
       <ons-icon icon="md-spinner" size="28px" spin></ons-icon><br><br> 
       <span id="message"></span><br><br> 
      </p> 
     </div> 
    </ons-modal> 

</body> 


</html> 

應用程序應該看起來像這樣: appearance in onsenui tutorial tool

但如果我捆綁與手機的差距應用它看起來像這樣: actual appearance on mobile device

我懷疑整個jquery代碼不能在移動設備上工作,但在onsenui教程工具中它正常工作。爲什麼觸摸事件和/或jQuery不適用於移動設備?謝謝

+0

有沒有可能是由jQuery和onsenui的CDN版本引起的問題? – radicz

回答

0

問題是,我只index.html的,無項目結構,配置文件等

我試圖去莫尼卡雲,創建了一個最小功能onsenui項目,並取代JavaScript代碼礦,用他們的工具構建apk,現在一切正常,佈局如預期,並且攻絲也是功能性的。希望對那些認爲你可以從一個html文件構建apk的人有所幫助,你需要所有的項目結構才能使它工作!