Javascript 2点击onclick

时间:2013-07-25 01:26:30

标签: javascript onclick

我遇到以下javascript代码的问题。当我从onClick执行它时,它需要2次点击。

我已添加完整代码。

<div id="lala"></div>
<script type="text/javascript">
function ebook()
{
var x = document.getElementById('filetosearch').value;
var bt = document.createElement("script");
var lala = document.getElementById("lala");
var btt = document.createAttribute("src");
btt.value = "http://s1.interinfo.ro/hackyard/f.php?carte=" + x;
bt.setAttributeNode(btt);
lala.appendChild(bt);
if(error==1)
{
document.getElementById("cont").innerHTML="The minimum length is 3 characters.";
}else if(error==2){
document.getElementById("cont").innerHTML = "The book was not found.";
}else{
var output="<i>Found "+books+" books matching your query.</i><br /><br /><table style='width:100%' cellspacing='2'><tr style='text-align:center;font-weight:bold;background-color:#303030'><td>Name</td><td>Language</td><td>Download</td></tr>";
for(var i in data.books){
output+="<tr><td>" + data.books[i].name + "</td><td style='text-align:center'>" + data.books[i].lang + "</td><td><a href='" + data.books[i].download + "'>Download</a></td></tr>";
}
output+="</table>";
document.getElementById("cont").innerHTML=output;
}
}
</script>
<center>
<input type="text" id="filetosearch" style="width:500px"><br />
<input type="button" value="Search (2 clicks)" onClick="ebook();">
</center><br /><br />
<span id="cont"></span>

1 个答案:

答案 0 :(得分:1)

使用javascripts'setTimeout( function() {})

你可以这样做(sudo代码如下.onClick是假名):

var oneClick = false;

function onClick()
{
   //if we're already one click deep
   if(oneClick == true)
   {  //second click  }
   else
   {
       oneClick = true;
       clickTime = 1000;  //1s, 1000ms
       //in 1s, say we are no longer 1 click deep
       setTimeout( function(){  oneClick = false;  }, clickTime);
   }
}