Prototype – 元素对象 cleanWhitespace() 方法

返回到:Prototype – 元素对象

此方法删除元素的所有文本节点,这些节点仅包含空格并返回元素。

Element.cleanWhitespace删除纯空白文本节点。这在使用nextSibling、previousSibling、firstChildlastChild等标准方法遍历 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 – 元素对象

作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9142.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月8日
下一篇 2023年2月8日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注