最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - get document children with javascript - Stack Overflow

programmeradmin7浏览0评论

I'm trying to get document first children (which means: body, html, head etc..) This code line works:

var x = document.body.children;

but this one does not:

var x = document.children;

how can I get the first childs of the document without running on all elements using JAVASCRIPT only?

I'm trying to get document first children (which means: body, html, head etc..) This code line works:

var x = document.body.children;

but this one does not:

var x = document.children;

how can I get the first childs of the document without running on all elements using JAVASCRIPT only?

Share Improve this question asked Sep 11, 2013 at 15:43 Doron CohenDoron Cohen 2456 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try childNodes:

document.childNodes

Note that document's children are <DOCTYPE> and <html>

The document element is appropriately called documentElement. In HTML, this is the <html> tag.

So:

document.documentElement.children

This should give you a list of length 2, with the first being the <head> and the second being the <body>.

If you use

document.childNodes

you get the docType and the html.

If you use

document.childNodes[1].childNodes

(supposing you have a doctype)

then you get the header and the body.

发布评论

评论列表(0)

  1. 暂无评论