Document.prototype.greenify = function(){
return {
style : function(){
return this.color = "green";
}
}
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
嘿,外面,
我想要建立一个“点函数”。我的审判职能应该是“绿化”我的元素。我已经尝试将函数添加到Window对象中,但结果是相同的。所以现在我的问题..。我做错了什么还是忘了什么?感谢我收到的每一个答案:)
发布于 2015-03-12 15:26:52
h1
元素不继承Document.prototype
。她们继承的是:
HTMLHeadingElement.prototype
HTMLElement.prototype
Element.prototype
Node.prototype
EventTarget.prototype
Object.prototype
例如,可以将该方法添加到HTMLElement.prototype
中。
HTMLElement.prototype.greenify = function(){
this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();
HTMLElement.prototype.greenify = function(){
this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
但是,请注意,修改不属于自己的对象被认为是一种糟糕的做法。
https://stackoverflow.com/questions/29013782
复制相似问题