1

我在manifest.json档案写了下面的代码被解雇:DOMContentReady不是在Chrome扩展

{ 
    "name":"SecondExtension",      
    "version":"1.0", 
    "manifest_version":2, 
    "content_security_policy": "script-src 'self';object-src 'self'", 
    "permissions":["tabs", "http://*/*", "https://*/*"], 
    "browser_action": { 
      "defaul_icon":"icon.png", 
      "default_title":"Security" , 
      "default_popup":"pops.html" 
    } 
} 

而且我在写pops.html下面的代码

<html> 
<head> 
<script src='popup.js'> 
</script> 
</head> 
<body> 

<p>Username : </p></br><input type="text" id="name" height="20" width="50" /> 

<p>Password :</p></br> <input type="password" id="password" height="20" width="50" /> 

<a href="pops2.html">login</a> 
<button id="login">login</button> 
<textarea id="return_name" rows="2" columns="20"></textarea> 
</body> 
</html> 

而且popup.js中的代码是:

function check() { 
    alert('its working'); 
} 
document.addEventListener('DOMContentReady', function() { 
    document.getElementById('login').addEventListener('click', check); 
}); 

现在的问题是JavaScript没有运行,因为每当我点击登录按钮不会提示“它正在工作”的消息。我在这里错过了什么?

回答

2

据我所知,没有DOMContentReady事件,有DOMContentLoaded

尝试用这种替代你的代码 -

function check() { 
    alert('its working'); 
}; 
document.addEventListener('DOMContentLoaded', function() { 
    document.getElementById('login').addEventListener('click', check); 
});