在Phonegap应用程序中不刷新的Javascript变量

时间:2011-07-17 18:16:00

标签: javascript cordova

我刚刚在phonegap中编写了第一个应用程序,每次激活链接时,它都会替换屏幕上的文本字符串。

原始字符串保持原样,新字符串写在顶部。如果您再次激活链接,则第二个字符串将替换为新字符串,但仍位于第一个字符串的顶部。

我已经尝试清除变量来解决这个问题,但没有运气。

这是一个平台限制还是我做错了什么?

代码在

之下
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="newIdea()">
<h1 class="h1">First Love</h1>
<p>Have you ever? </p>
<h3><div id="ideaDiv">Nothing</div></h3>
<a href="#" id="ideaLink">Let's Do it</a>
<a href="#" onclick="newIdea();">No Thanks</a>
<script type="text/javascript">
var ideas=new Array(); // regular array (add an optional integer
ideas[0]="Kissed someone in the rain";       // argument to control array's size)
ideas[1]="Eaten peking duck";
ideas[2]="Stood naked in the open";

function newIdea(){
var idea = "";
var idea = ideas[Math.floor(Math.random()*ideas.length)];
var ideaSpace = document.getElementById("ideaDiv");
ideaSpace.innerHTML=idea;
var ideaLink=document.getElementById("ideaLink");
var linkCreate="http://www.google.com/calendar/event?action=TEMPLATE&text=" + idea + "&dates=20120101/20120102&details=&location=&trp=false&sprop=&sprop=name:";
ideaLink.href=linkCreate;

}
</script>
</body>
</html>

由于

西蒙

1 个答案:

答案 0 :(得分:0)

我没有使用phonegap的经验,但过去我发现在xhtml文档中设置innerHTML时遇到了一些问题,它不检查你使用的字符串是否导致文档仍然是有效的xml和只是抛出一个错误,达到同样的效果试试:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="newIdea()">
<h1 class="h1">First Love</h1>
<p>Have you ever? </p>
<h3><div id="ideaDiv">Nothing</div></h3>
<a href="#" id="ideaLink">Let's Do it</a>
<a href="#" onclick="newIdea();">No Thanks</a>
<script type="text/javascript">
var ideas=new Array(); // regular array (add an optional integer
ideas[0]="Kissed someone in the rain";       // argument to control array's size)
ideas[1]="Eaten peking duck";
ideas[2]="Stood naked in the open";

function newIdea(){
var idea = "";
var idea = ideas[Math.floor(Math.random()*ideas.length)];
var ideaSpace = document.getElementById("ideaDiv");
//ideaSpace.innerHTML=idea;
ideaSpace.removeChild(ideaSpace.firstChild);
ideaSpace.appendChild(document.createTextNode(idea));
var ideaLink=document.getElementById("ideaLink");
var linkCreate="http://www.google.com/calendar/event?action=TEMPLATE&text=" + idea + "&dates=20120101/20120102&details=&location=&trp=false&sprop=&sprop=name:";
ideaLink.href=linkCreate;

}
</script>
</body>
</html>