Prototype - insert() 方法

此方法根据第二个参数的 position 属性在元素之前、之后、顶部或底部插入内容。如果第二个参数是内容本身,则 insert 将 附加 到元素。

Insert 接受以下类型的内容 −

  • 文本
  • HTML
  • DOM 元素
  • 具有 toHTML 或 toElement 方法的任何类型的对象。

注意 − 请注意,如果插入的 HTML 包含任何 <script> 标签,则这些标签将在插入后自动评估。

语法

element.insert({ position: content });

或

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>单击按钮查看结果。</p>
      
      <ul>
         <li id = "apple">apple</li>
         <li>orange</li>
      </ul>
      <br />
      
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_element_object.html