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