TypeError:值不实现接口EventListener [英] TypeError: Value does not implement interface EventListener
问题描述
$ b
TypeError:Value没有实现接口EventListener。
这段代码是由php脚本发布的,所以我不得不添加if语句来确定id是否存在,首先要避免在Chrome中出错。
以下是代码:
HTML
< a id =注销>注销< / a>
JavaScript
if(document.getElementById(Logout)!= null){
var Logout_Link = document.getElementById(Logout);
Logout_Link.addEventListener('click',注销,true);
函数Logout(){
var just_logged_out = 1;
window.location.href =logout-redirect.php;
$ div $解析方案
$如果(...){
函数...(){
...
}
}
code>
无效。函数声明必须位于函数或程序体的顶层,位于块的内部他们的行为是依赖于实现的。把它移到if语句之外: 所以如果函数没有被正确的声明,奇怪的错误信息是什么?你可能已经期望像 This works find in IE and Chrome, however, in Firefox, I get the error: "TypeError: Value does not implement interface EventListener." This code is posted by a php script, so I had to add the if statement determining if the id exists, first, to avoid errors in Chrome. Here's the code: HTML
JavaScript
$ p $ function Logout(){
var just_logged_out = 1;
window.location.href =logout-redirect.php;
if(document.getElementById(Logout)!= null){
var Logout_Link = document.getElementById(Logout);
Logout_Link.addEventListener('click',注销,true);
$ b 错误的自变量错误:处理程序是'undefined'
。但实际上不是, 注销 code>确实指向您的链接元素
。这确实没有实现 EventListener
接口(JavaScript函数可以)。<a id="Logout">Logout</a>
if (document.getElementById("Logout") != null) {
var Logout_Link = document.getElementById("Logout");
Logout_Link.addEventListener('click', Logout, true);
function Logout() {
var just_logged_out = 1;
window.location.href = "logout-redirect.php";
}
}
if (…) {
function …() {
…
}
}
is invalid. Function declarations must be on the top level of function or program bodies, inside of blocks their behaviour is implementation-dependent. Move it outside the if-statement:
function Logout() {
var just_logged_out = 1;
window.location.href = "logout-redirect.php";
}
if (document.getElementById("Logout") != null) {
var Logout_Link = document.getElementById("Logout");
Logout_Link.addEventListener('click', Logout, true);
}
So if the function was not declared [correctly], what's that odd error message? You might've expected something like WRONG ARGUMENTS ERROR: handler is 'undefined'
. But actually it's not, Logout
does refer to your link element. And that does indeed not implement the EventListener
interface (JavaScript functions do).
这篇关于TypeError:值不实现接口EventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!