2014-03-27 46 views
2

我想用Phonegap做一個簡單的警報框,它已經很難實現。在觀看了很多教程之後,我無法使其工作,仿真器和設備內測試也無法正常工作。Phonegap:不能得到一個簡單的警報框工作

所以我畫了3個div,每個都是一個不同顏色的盒子。然後,當我單擊它時,我試着對每個單獨的警告框做一個簡單的提示框,使用不同的代碼。當Eclipse將Apk加載到我的手機中時,它不會構建當前的工作版本,有時它也會。當它發生時,我可以嘗試應用程序,然後意識到沒有任何點擊事件發揮作用。

這裏是我的代碼:

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="format-detection" content="telephone=no" /> 
     <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Testing</title> 
    </head> 
    <body onload="DOMloaded()"> 
     <div id="box" onclick="funcClick()"></div> 

     <div id="box2"></div> 

     <div id="box3"></div> 



     <script type="text/javascript" src="cordova.js"></script> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 

     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

index.css

#box { 
    width: 300px; 
    height: 300px; 
    background-color: yellow; 
} 
#box2 { 
    width: 300px; 
    height: 300px; 
    background-color: red; 
} 

#box3 { 
    width: 300px; 
    height: 300px; 
    background-color: blue; 
    display: inline-block; 
} 

index.js

function DOMloaded() { 
    document.addEventListener("deviceready", phonegapLoaded, false); 
} 

function phonegapLoaded() { 
    function funcClick() { 
     alert("Yellow box onclick event"); 
    }; 
    $("#box2").click(function() { 
     alert("Red box jQuery click event"); 
    }); 
    $("#box3").on('touchstart', function() { 
     alert("Blue box jQuery and Phonegap touch event"); 
    }); 
    alert("Events loaded"); 
} 

有工作的唯一的事alert("Events loaded").

請幫幫我。

回答

0

您在本地範圍中定義了「funcClick」(在「phonegapLoaded」函數的範圍內),因此它不適用於div點擊。我不能給出很好的解釋(也許會有人在評論中解釋),爲什麼沒有任何反應與BOX2和BOX3,但我有工作的解決方案:

function funcClick() { 
    alert("Yellow box onclick event"); 
}; 

function phonegapLoaded() { 
    $(document).on("click", "#box2", function(){ 
     alert("Red box jQuery click event"); 
    }); 

    $(document).on('touchstart', "#box3", function(){ 
     alert("Blue box jQuery and Phonegap touch event"); 
    }); 
    alert("Events loaded"); 
} 
+0

的onclick已經工作,但是當我在我的手機上安裝它在這些300毫秒的延遲,即使我使用全局範圍,也不能使用這些點擊和觸摸啓動。有任何想法嗎? – user3062745

+0

@ user3062745我花費大量時間試圖弄清楚如何提高Cordova + JQM應用程序的性能(包括無抽頭延遲)。但我仍然不能說任何有用的東西 – Regent

+0

我現在已經解決了 – user3062745