原型 - 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
广告