原型 - 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>
輸出
prototype_element_object.htm
廣告