0

我对以下 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>
4

1 回答 1

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);
   }
}
于 2013-07-25T01:33:38.673 回答