原型 - insert() 方法



此方法在元素之前、之後、頂部或底部插入內容,具體取決於第二個引數的 position 屬性。如果第二個引數是內容本身,insert會將其追加到元素中。

Insert接受下列型別的內容 −

  • 文字
  • HTML
  • DOM 元素
  • 任何帶有 toHTML 或 toElement 方法的物件。

注意 − 請注意,如果插入的 HTML 包含任何 <script> 標籤,則這些標籤將在插入後自動評估。

語法

element.insert({ position: content });

OR

element.insert(content)

返回值

返回插入內容後的 HTML 元素。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = $('apple').insert(  "<li>mangoes</li>" );
            alert(str.innerHTML );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <ul>
         <li id = "apple">apple</li>
         <li>orange</li>
      </ul>
      <br />
      
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_element_object.htm
廣告