返回到:Prototype – 元素对象
此方法删除元素的所有文本节点,这些节点仅包含空格并返回元素。
Element.cleanWhitespace删除纯空白文本节点。这在使用nextSibling、previousSibling、firstChild或lastChild等标准方法遍历 DOM 时非常有用。
语法
element.cleanWhitespace();
返回值
一个 HTML 元素
例子
考虑以下示例:
HTML
x
24
24
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showElements() {
8
var element = $('apples');
9
alert(element.firstChild.innerHTML);
10
}
11
</script>
12
</head>
13
14
<body>
15
<ul id = "apples">
16
<li>Mutsu</li>
17
<li>McIntosh</li>
18
<li>Ida Red</li>
19
</ul>
20
<br />
21
22
<input type = "button" value = "showElements" onclick = "showElements();"/>
23
</body>
24
</html>
这似乎不太奏效。这是为什么 ?ul#apples 的第一个子节点实际上是一个仅包含位于 <ul id = “apples”> 和 <li>Mutsu</li> 之间的空白的文本节点。
现在,让我们使用 cleanWhitespace 函数并查看结果:
HTML
xxxxxxxxxx
1
26
26
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showElements() {
8
var element = $('apples');
9
element.cleanWhitespace();
10
alert(element.firstChild.innerHTML);
11
}
12
</script>
13
</head>
14
15
<body>
16
<ul id = "apples">
17
<li>Mutsu</li>
18
<li>McIntosh</li>
19
<li>Ida Red</li>
20
</ul>
21
<br />
22
23
<input type = "button" value = "showElements" onclick = "showElements();"/>
24
</body>
25
</html>
26
返回到:Prototype – 元素对象
阅读剩余 63%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9142.html