简体   繁体   English

为什么不能通过document.getElementById(“ node's_id”)。child选择子节点?

[英]Why can't I select nodes child by document.getElementById(“node's_id”).child?

Suppose I have a simple html code sonsisiting a parent div and child span element as: 假设我有一个简单的html代码,将父div和子span元素设置为:

<div id="my_div"><span> Nothin' but the G thang </span></div>

It is said that every element in html DOM is an object. 据说html DOM中的每个元素都是一个对象。 So we can select child objects as parent.child . 因此,我们可以选择子对象作为parent.child Eg window.document , window.document.body , window.document.head etc then my question is why does document.getElementById("my_div").span return undefined? 例如window.documentwindow.document.bodywindow.document.head等,那么我的问题是为什么document.getElementById("my_div").span返回未定义?

Similarly window.document.body works but I can't go one level down after that with window.document.body.div . 同样, window.document.body可以,但是在window.document.body.div之后,我不能再下一层了。

With . . ( dot-notation ), you are trying to access the span property of the object ( Node/Element ) returned by document.getElementById . dot-notation ),您正在尝试访问document.getElementById返回的objectNode/Element )的span属性。 As there is no span property associated with returned object, document.getElementById("my_div").span will be undefined 由于没有与返回的对象关联的span属性,因此document.getElementById("my_div").span将是undefined

Use document.querySelector , Returns the first element within the document (using depth-first pre-order traversal of the document's nodes|by first element in document markup and iterating through sequential nodes by order of amount of child nodes) that matches the specified group of selectors 使用document.querySelector返回与指定组匹配的 document.querySelector 中的第一个元素(使用文档标记中的第一个元素,通过深度优先顺序遍历文档的节点|通过子节点数量的顺序遍历顺序节点)选择器

 console.log(document.querySelector('#my_div span')); 
 <div id="my_div"><span> Nothin' but the G thang </span> </div> 

Or Node.chilren , read-only property that returns a live HTMLCollection of the child elements of Node. Node.chilren ,它是只读属性,它返回Node的子元素的实时HTMLCollection。

 console.log(document.getElementById('my_div').children[0]); 
 <div id="my_div"><span> Nothin' but the G thang </span> </div> 

You can select span like this 您可以选择像这样的跨度

 var span = document.getElementById("my_div").querySelector('span'); console.log(span); 
 <div id="my_div"><span> Nothin' but the G thang </span> </div> 

Or in your case you can also use firstChild 或者您也可以使用firstChild

 var span = document.getElementById("my_div").firstChild; console.log(span); 
 <div id="my_div"><span>Nothin' but the G thang </span></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在document.getelementbyID内传递多个ID吗? - Can I pass multiple Id's inside a document.getelementbyID? 为什么我的if语句不能以document.getElementById(“ ID”)。value = var结尾? - Why can't I end my if statement with document.getElementById(“ID”).value = var? 使用Javascript的document.getElementByID来获取div的子节点 - Using Javascript's document.getElementByID to get child of a div 与 jQuery 的 $() 不同,由于 document.getElementById() 和 document.querySelector() 不返回节点,因此无法在 vanilla JS 上附加子项 - Unable to append child on vanilla JS due to document.getElementById() and document.querySelector() not returning nodes, unlike jQuery's $() 为什么querySelector('#id')映射到document.getElementById('id')? - Why doesn't querySelector('#id') map to document.getElementById('id')? "document.getElementById 不能选择多个元素" - document.getElementById can't select more than one element JQuery select 按 ID 与 document.GetElementByID - JQuery select by ID vs document.GetElementByID 我应该怎样包装document.getElementById以便在找不到ID时不会引发TypeError? - What do I wrap a document.getElementById so as not to throw a TypeError when ID can't be found? 为什么不能设置`document.getElementById(“ text”); `要全球化? - Why can't set `document.getElementById(“text”); ` to be global? 为什么不能使用document.getElementById替换JavaScript中的$? - Why can't use document.getElementById to replace $ in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM