原型 - cleanWhitespace() 方法
此方法刪除元素的所有僅包含空白的文字節點,並返回該元素。
Element.cleanWhitespace 刪除僅包含空白的文字節點。在使用標準方法(例如 nextSibling, previousSibling, firstChild 或 lastChild)遍歷 DOM 時,可能會非常有用。
語法
element.cleanWhitespace();
返回值
一個 HTML 元素
示例
考慮以下示例 −
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showElements() {
var element = $('apples');
alert(element.firstChild.innerHTML);
}
</script>
</head>
<body>
<ul id = "apples">
<li>Mutsu</li>
<li>McIntosh</li>
<li>Ida Red</li>
</ul>
<br />
<input type = "button" value = "showElements" onclick = "showElements();"/>
</body>
</html>
這似乎不怎麼管用。為什麼呢?ul#apples 的第一個子元素實際上是一個僅包含“<ul id =”apples“>”與“<li>Mutsu</li>”之間的空白的文字節點。
輸出
現在,我們使用 cleanWhitespace 函式並檢視結果 −
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showElements() {
var element = $('apples');
element.cleanWhitespace();
alert(element.firstChild.innerHTML);
}
</script>
</head>
<body>
<ul id = "apples">
<li>Mutsu</li>
<li>McIntosh</li>
<li>Ida Red</li>
</ul>
<br />
<input type = "button" value = "showElements" onclick = "showElements();"/>
</body>
</html>
輸出
prototype_element_object.htm
廣告