javascript如何元素当前节点的父节点:
在实际编码的时候可能需要获得当前节点的父节点进行操作,下面就简单介绍一下如何实现此功能。
代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <head> <title>-蚂蚁部落</title> <style type="text/css"> #father { width:200px; height:200px; padding:20px; } #children { width:80px; height:80px; background-color:green; } </style> <script type="text/javascript"> window.onload=function() { var children=document.getElementById("children"); var father=children.parentNode; father.style.border="5px solid red"; } </script> </head> <body> <div id="father"> <div id="children"></div> </div> </body> </html>
以上代码可以设置父div的边框属性。使用parentNode即可获取父节点,能够兼容所有主流浏览器。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8255
更多内容可以参阅:http://www.softwhy.com/javascript/