原型 - addMethods() 方法
此方法可将您自己的方法混合到元素对象中,您以后可以用它作为扩展元素的方法。
要添加新方法,只需用方法杂凑馈送 Element.addMethods 即可。请注意,每个方法的第一个参数都必须是一个元素。
语法
element.addMethods([hash of methods]); OR element.addMethods(tagName, methods);
在此,此方法的第二个形式会仅对特定标签提供添加的方法。
返回值
无。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
// Make changeColor method available for all the elements
Element.addMethods({
changeColor: function(element, colorName) {
element = $(element);
element.style.color = colorName;
return element;
}
});
function ShowEffect() {
node = $("firstDiv");
// Now call changeColor method
node.changeColor( "red" );
}
</script>
</head>
<body>
<div id = "firstDiv">
<p>This is first paragraph</p>
</div>
<br />
<input type = "button" value = "ShowEffect" onclick = "ShowEffect();"/>
</body>
</html>
输出
prototype_element_object.htm
广告