2016-12-14 99 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>

相关问题