前端学习笔记:DOM之elemChildren

it2025-09-04  2

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>课时1</title> </head> <body> <div class="box" id="box" style="background-color: green"> 我是文本节点 <!-- 我是注释君 --> <h1>我是标题标签</h1> <a href="">我是超链接</a> <p>我是段落标签</p> </div> <script type="text/javascript"> var div = document.getElementsByTagName('div')[0]; function elemChildren(node){ var temp = { 'length': 0, 'push': Array.prototype.push, 'splice': Array.prototype.splice }, len = node.childNodes.length; for(var i = 0; i < len; i++){ var childItem = node.childNodes[i]; if(childItem.nodeType === 1){ temp[temp['length']] = childItem; temp['length']++; } } return temp; } console.log(elemChildren(div)); </script> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>课时1</title> </head> <body> <div class="box" id="box" style="background-color: green"> 我是文本节点 <!-- 我是注释君 --> <h1>我是标题标签</h1> <a href="">我是超链接</a> <p>我是段落标签</p> </div> <script type="text/javascript"> var div = document.getElementsByTagName('div')[0]; function elemChildren(node){ var temp = { 'length': 0, 'push': Array.prototype.push, 'splice': Array.prototype.splice }, len = node.childNodes.length; for(var i = 0; i < len; i++){ var childItem = node.childNodes[i]; if(childItem.nodeType === 1){ temp[temp['length']] = childItem; temp['length']++; } } return temp; } console.log(elemChildren(div)); </script> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>课时1</title> </head> <body> <div class="box" id="box" style="background-color: green"> 我是文本节点 <!-- 我是注释君 --> <h1>我是标题标签</h1> <a href="">我是超链接</a> <p>我是段落标签</p> </div> <script type="text/javascript"> var div = document.getElementsByTagName('div')[0]; function elemChildren(node){ var temp = { 'length': 0, 'push': Array.prototype.push, 'splice': Array.prototype.splice }, len = node.childNodes.length; for(var i = 0; i < len; i++){ var childItem = node.childNodes[i]; if(childItem.nodeType === 1){ temp.push(childItem); } } return temp; } console.log(elemChildren(div)); </script> </body> </html>
最新回复(0)