返回到:Prototype – 元素对象
此方法可以将您自己的方法混合到 Element 对象中,稍后您可以将其用作扩展元素的方法。
要添加新方法,只需向 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 – 元素对象
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9154.html
支付宝
微信