简体   繁体   中英

how can I change the js built-in objects properties?

I do this: Element.toggle = Element.classList.toggle and I get an error. how can I do this in the right way? thanks.

You can add to the HTMLElement prototype

 HTMLElement.prototype.toggle = function (cls, flag) { this.classList.toggle(cls, flag) } var test = document.querySelector("#test") test.toggle('yellow')
 div.yellow { background-color: yellow; }
 <div id="test">Hello</div>

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