返回到:Prototype – 元素对象
此方法可以将您自己的方法混合到 Element 对象中,稍后您可以将其用作扩展元素的方法。
要添加新方法,只需向 Element.addMethods 提供方法的散列。请注意,每个方法的第一个参数必须是一个元素。
语法
element.addMethods([hash of methods]); OR element.addMethods(tagName, methods);
在这里,方法的第二种形式将使添加的方法仅可用于特定标记。
返回值
没有任何。
例子
HTML
x
32
32
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
// Make changeColor method available for all the elements
8
Element.addMethods({
9
changeColor: function(element, colorName) {
10
element = $(element);
11
element.style.color = colorName;
12
return element;
13
}
14
});
15
function ShowEffect() {
16
node = $("firstDiv");
17
18
// Now call changeColor method
19
node.changeColor( "red" );
20
}
21
</script>
22
</head>
23
24
<body>
25
<div id = "firstDiv">
26
<p>This is first paragraph</p>
27
</div>
28
<br />
29
30
<input type = "button" value = "ShowEffect" onclick = "ShowEffect();"/>
31
</body>
32
</html>
返回到:Prototype – 元素对象
阅读剩余 82%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9154.html