Prototype - toggleClassName() 方法



此方法切换元素的 CSS className 并返回元素。

语法

element.toggleClassName( className );

返回值

返回一个 HTML 元素。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            if( !$('mutsu').hasClassName('fruit') ) {
               // This will return false
               alert( "Class name is not set" );
            }
            $('mutsu').toggleClassName('fruit');
            
            // This will toggle the class presence
            if( $('mutsu').hasClassName('fruit') ) {
               // This returns true
               alert( "Now Class name is set" );
            }
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <div id = "mutsu" class = "apple">
         <p>This is test division.</p>
      </div>
      <br />
      
      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_element_object.htm
广告