Prototype-element-replace

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

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

このメソッドは、要素をhtml引数のコンテンツで置き換え、削除された要素を返します。

構文

element.replace(html);

ここで、_html_はプレーンテキスト、HTMLスニペット、またはtoString()メソッドを持つJavaScriptオブジェクトのいずれかです。

<script>タグが含まれている場合、これらは要素が置換された後に評価されます。

-引数が指定されていない場合、Element.replaceはコンテンツの要素を単純にクリアします。

戻り値

削除されたHTML要素を返します。

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

      <script>
         function showResult() {
            $('first').replace('<ul id = "favorite">' +
               '<li>kiwi</li>' +
               '<li>banana</li>' +
               '<li>apple</li>' +
            '</ul>');
         }
      </script>
   </head>

   <body">
      <p id = "test">Click the button to see the result.</p>

      <div id = "food">
         <div id = "fruits">
            <p id = "first">Kiwi, banana <em>and</em> apple.</p>
         </div>
      </div>

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

出力