Prototype-element-cleanwhitespace

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

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

このメソッドは、空白のみを含む要素のテキストノードをすべて削除し、要素を返します。

Element.cleanWhitespace_は、空白のみのテキストノードを削除します。 これは、_nextSibling、previousSibling、firstChild、または_lastChild_などの標準メソッドを使用してDOMをウォークするときに非常に便利です。

構文

element.cleanWhitespace();

戻り値

HTML要素

次の例を考慮してください-

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

      <script>
         function showElements() {
            var element = $('apples');
            alert(element.firstChild.innerHTML);
         }
      </script>
   </head>

   <body>
      <ul id = "apples">
         <li>Mutsu</li>
         <li>McIntosh</li>
         <li>Ida Red</li>
      </ul>
      <br/>

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

それはうまく機能していないようです。 何故ですか ? ul#applesの最初の子は、実際には<ul id = "apples">と<li> Mutsu </li>の間にある空白のみを含むテキストノードです。

出力

さて、cleanWhitespace関数を使用して結果を見てみましょう-

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

      <script>
         function showElements() {
            var element = $('apples');
            element.cleanWhitespace();
            alert(element.firstChild.innerHTML);
         }
      </script>
   </head>

   <body>
      <ul id = "apples">
         <li>Mutsu</li>
         <li>McIntosh</li>
         <li>Ida Red</li>
      </ul>
      <br/>

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

出力