Prototype-element-hasclassname

提供:Dev Guides
移動先:案内検索

プロトタイプ-hasClassName()メソッド

このメソッドは、要素に特定のCSS classNameがあるかどうかを確認します。

構文

element.hasClassName( className );

戻り値

要素に指定されたクラス名がある場合、trueを返します。それ以外の場合はfalseを返します。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            if( $('grandfather').hasClassName("test") ) {
               alert("Grandfather has CSS test class.");
            }
            if( $('father').hasClassName("test") ) {
               alert("Father has CSS test class.");
            }
            if( $('kid').hasClassName("test") ) {
               alert("Kid has CSS test class.");
            }
         }
      </script>

      <style>
         .test {
            font-size: 12px;
            margin-left: 1em;
         }
      </style>
   </head>

   <body>
      <p>Click the button to see the result.</p>

      <div id = "grandfather" class = "test">This is grand father
         <div id = "father"> This is father.
            <div id = "kid" class = "test">This is kid</div>
         </div>
      </div>
      <br/>

      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

出力