简体   繁体   中英

How to add instance of javascript Class in a DOM element

I need to expose the istance of a class to be called from the dom element.

i've created a class that i'll instance as new myCustomClass() . This class is related to a DOM element.

What i need to do in my class to access to his methods and property form the dom?

I need something like this (this is something that swiper.js do)

var foo = document.querySelector('.element-where-class-is-instanced').myCustomClass;
foo.init()

thanks to all

Just assign the class instance to the element property earlier:

document.querySelector('.element-where-class-is-instanced').myCustomClass = new myCustomClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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