2014-03-27 26 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