原型 - 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
廣告