Prototype – 元素对象 descendantOf() 方法

返回到:Prototype – 元素对象

此方法检查元素是否是祖先的后代。

由于 Element.descendantOf 在内部将 $() 应用于祖先,它无差别地接受一个元素或一个元素的 id 作为它的第二个参数。

语法

element.descendantOf(ancestor);

返回值

如果它发现该元素是祖先的后代,则返回 true,否则返回 false。

例子

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function isDescendant() {
            var father = $('father');
            var kid = $('kid');
            
            // This is correct relationship and will be printed
            if( kid.descendantOf(father) ) {
               alert( "Kid is descendant of father" );
            }
            
            // This is wrong relationship and will not be printed
            if( father.descendantOf(kid) ) {
               alert( "Father is descendant of kid" );
            }
         }
      </script>
   </head>

   <body>
      <p>Click isDescendant button to see the result.</p>
      <div id = "grandfather">
         <div id = "father">
            <div id = "kid"></div>
         </div>
      </div>
      <br />
      
      <input type = "button" value = "isDescendant" onclick = "isDescendant();"/>
   </body>
</html>

返回到:Prototype – 元素对象

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

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

相关推荐

发表回复

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