javascript
2013-10-18 34 views 0 likes 
0

硬盤在人類的語言來解釋,只顯示代碼:無法點擊對方一再

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'> 
<head> 
    <script type="text/javascript"> 
     function call_f1(){ 
      document.getElementById('f1_div').click(); 

      // document.getElementById('f1_div').click(); 
     } 

     function f1(){ 
      console.log('f1 '+new Date());    
      document.getElementById('btn_1').click();     
     } 

    </script> 
</head> 
<body> 
    <input id="btn_1" type="button" onclick="call_f1()" value="callf1"></input> 
    <div id="f1_div" onclick="f1()" style="background: red; width: 35px; height: 35px;"> 
    </div> 
</body> 

當您使用鼠標單擊該按鈕,你得到的控制檯上的單一輸出。爲什麼??重複調用f1函數嗎?

+0

首先你爲什麼會想這樣做,其次,爲什麼不使用一個循環,如果你想要的是被記錄的日期? –

+0

你確定* div *有* click()*方法嗎?你的遊戲機說什麼? –

+1

@拉梅達鴨所有HTML元素都應該具有'click'功能:https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.click –

回答

1

爲什麼?重複調用f1函數嗎?

.click()並在啓動時已經有對元素的活性'click'事件將不會做任何事情synthetic click activation

  1. 如果元素的點擊正在進行標誌被設置爲true,那麼跳過這些步驟

  2. 點擊進行中設置爲true的標誌。

如果你希望它是與.click()連續的,你得把以前的事件先完成。可能與使用定時器:

function f1(){ 
    console.log('f1 '+new Date()); 

    setTimeout(function() { 
     document.getElementById('btn_1').click(); 
    }, 1); 
} 
1

嘗試:

function call_f1(){ 
     document.getElementById('f1_div').onclick(); 

     // document.getElementById('f1_div').onclick(); 
    } 

    function f1(){ 
     console.log('f1 '+new Date());    
     document.getElementById('btn_1').onclick();     
    } 

這一個是爲我工作:http://jsfiddle.net/djhell/rG782/

+1

看起來像Firefox或jsFiddle檢測到無限遞歸併停止它,但絕對有效。 –

2

http://www.w3.org/TR/html5/dom.html#run-synthetic-click-activation-steps 因此,添加超時兩個函數調用。如下。

var timeout = 0; // change this to whatever suites for you 
    function call_f1(){ 
     setTimeout(function(){ 
      document.getElementById('f1_div').click(); 
     }, timeout); 

     // document.getElementById('f1_div').click(); 
    } 

    function f1(){ 
     console.log('f1 '+new Date());    
     setTimeout(function() { 
      document.getElementById('btn_1').click(); 
     }, timeout);     
    } 
相關問題