简体   繁体   English

如何在类EcmaScript6中使用addEventListener?

[英]How can I use addEventListener in Class EcmaScript6?

I use EcmaScript6 and don't know how to apply addEventListener in a Class. 我使用EcmaScript6,但不知道如何在Class中应用addEventListener。 I tried declaring a var out of the Class to call the method EventListener and got an Error :/. 我试图从类中声明一个var来调用方法EventListener并得到一个错误:/。 I also tried addEventlistener inside of the Class and I can't. 我还尝试了在类内部的addEventlistener,但我做不到。

The errors in console, is because I'm try again new alternate. 控制台中的错误是因为我再次尝试新的替代方法。

Don't know solution problem. 不知道解决问题。 how add the event listener ? 如何添加事件监听器?

<!-- language: lang-js -->
class Design{
transition(clickea, up, down){
    this.clickea = clickea;
    this.up = up;
    this.down = down;
    let cont = 0;
    let focus = document.getElementById(clickea);
    let content = document.getElementById(up);
    let content2 = document.getElementById(down);
    if(cont == 0){
        content.style.transform = "translateY(-1000%)";
        content2.style.transform = "translateY(0%)";
        cont++;
    }else{
        content.style.transform = "translateY(0%)";
        content2.style.transform = "translateY(-100%)";
        cont--;
    }
}
}

const design = new Design();
let enlance = document.getElementById('sign');
enlance.addEventListener('click', desgin.transition(enlance, loguin, 
registro));

Google console Google控制台

eventos.js:11 Uncaught TypeError: Cannot read property 'style' of null
at Design.transition (eventos.js:11)
at eventos.js:24

or 要么

<!-- language: lang-js -->
class Design{
transition(clickea, up, down){
    this.clickea = clickea;
    this.up = up;
    this.down = down;
    let cont = 0;
    let focus = document.getElementById(clickea);
    let content = document.getElementById(up);
    let content2 = document.getElementById(down);
    focus.addEventListener('click', function(up,down){
        if(cont == 0){
            content.style.transform = "translateY(-1000%)";
            content2.style.transform = "translateY(0%)";
            cont++;
        }else{
            content.style.transform = "translateY(0%)";
            content2.style.transform = "translateY(-100%)";
            cont--;
        }
    });

}

} }

const diseño = new Design();
let enlance = document.getElementById('sign');
let loguin = document.getElementById('loguin');
let registro = document.getElementById('registro');
diseño.transition(enlance, loguin, registro);


eventos.js:10 Uncaught TypeError: Cannot read property 'addEventListener' of null
    at Design.transition (eventos.js:10)
    at eventos.js:29
transition @ eventos.js:10

My question is, how i can use listener in the template method Class. 我的问题是,如何在模板方法Class中使用侦听器。

Welcome to Stackoverflow! 欢迎来到Stackoverflow!

It looks like you're seeing this error because your class is expecting a string, but you're providing a DOM chunk. 您似乎正在看到此错误,因为您的类需要一个字符串,但是您正在提供一个DOM块。

Your code is essentially doing this... 您的代码本质上就是这样做的。

document.getElementById(document.getElementById('sign'));

If you just provide strings to transition() you should be good. 如果只提供字符串给transition() ,那应该很好。

Regarding your actual question 关于你的实际问题

How can I use addEventListener in Class EcmaScript6? 如何在类EcmaScript6中使用addEventListener?

A class is really no different than a function, its just a way to store data/functionality. 类实际上与函数没有什么不同,它只是存储数据/功能的一种方式。 You can have a method on the class that gets a piece of DOM or a adds a listener to a piece of DOM, but you can't put a listener directly on the class. 您可以在类上有一个获取一个DOM的方法,或者在一个DOM上添加一个侦听器,但是您不能将侦听器直接放在类上。 As it sits right now, it doesn't look like you are even trying to do that though. 现在,它看起来似乎还没有尝试。 Your class simply stores some logic and procedural DOM code which is totally valid. 您的类仅存储一些完全有效的逻辑和过程DOM代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM