原型 - descendantOf()方法
此方法检查元素是否是祖先的后代。
由于 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_element_object.htm
广告