TypeError:值不实现接口EventListener [英] TypeError: Value does not implement interface EventListener

查看:160
本文介绍了TypeError:值不实现接口EventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个工程在IE和Chrome中找到,但是,在Firefox中,我得到的错误:
$ 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语句之外:

$ 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函数可以)。


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 <a id="Logout">Logout</a>

JavaScript

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆