是否有可能通过JavaScript通过自定义属性或直接通过其构造函数访问DOM元素(最好是HTMLElement
)?通过构造函数属性访问DOM元素
类似下面的代码:
/* Some Attribute
document.body.someAttribute == document.body
(this should be true)
*/
HTMLElement.prototype.someAttribute = (function() {
/* Return the element. */
})();
不确定的用例,但这里是如何选择DOM元素。 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector – arodjabel
“通过其构造函数访问DOM元素”你想用这个做什么?从我的看法来看,如果没有DOM元素,你将无法访问这个方法,所以提供一种让自己远离自身的方式似乎是多余的,除非你试图访问元素的一个属性,在这种情况下[ 'Element.getAttribute()'](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute)存在 –
可能与[如何将自己的方法添加到HTMLElement对象相同? ](https://stackoverflow.com/q/4670361/215552),但很难说... –