简体   繁体   English

如何更改 js 内置对象属性?

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

I do this: Element.toggle = Element.classList.toggle and I get an error.我这样做: Element.toggle = Element.classList.toggle我得到一个错误。 how can I do this in the right way?我怎样才能以正确的方式做到这一点? thanks.谢谢。

You can add to the HTMLElement prototype您可以添加到 HTMLElement 原型

 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>

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

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