2016-12-14 96 views
0

嗨,我正在創建一個Chrome應用程序。我使用java腳本在按鈕上創建了一個點擊事件。它在簡單的html頁面中工作正常,但不適用於chrome應用程序。onclick不適用於Chrome應用程序?

<!DOCTYPE html> 
 
<html> 
 
    <body> 
 
     <form> 
 
      <input type="button" id="btn01" value="OK"> 
 
     </form> 
 

 
     <p>Click the "Disable" button to disable the "OK" button:</p> 
 

 
     <button onclick="disableElement()">Disable</button> 
 

 
     <script> 
 
      function disableElement() { 
 
       document.getElementById("btn01").disabled = true; 
 
      } 
 
     </script> 
 
    </body> 
 
</html>

+0

我不認爲onclick事件是與移動環境兼容。 –

+0

你是否檢查過這個帖子, http://stackoverflow.com/questions/13591983/onclick-within-chrome-extension-not-working你應該添加事件監聽器。 –

回答

0

你不能在Chrome擴展加載內嵌的JavaScript。相反,您需要創建一個可以添加事件偵聽器的外部JavaScript文件。事情是這樣的:

document.addEventListener('DOMContentLoaded', function() { 
 
document.getElementById('disable-button').addEventListener('click', function() { 
 
     document.getElementById("btn01").disabled = true; 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<body> 
 
    <form> 
 
    <input type="button" id="btn01" value="OK"> 
 
    </form> 
 

 
    <p>Click the "Disable" button to disable the "OK" button:</p> 
 
    <button id="disable-button">Disable</button> 
 
</body> 
 
</html>

相關問題