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.