简体   繁体   中英

How do I get access to the <html> element in Javascript?

IE displays a default scrollbar on the page, which appears even if the content is too short to require a scrollbar.

The typical way to remove this scrollbar (if not needed), is to add this to your CSS:

html {
  height: 100%;
  overflow: auto;
}

I'm trying to do the same thing in Javascript (without requiring that in my CSS), but I can't seem to find a way to get access to the <html> element. I know I can access the <body> element with document.body , but that doesn't seem to be sufficient, I need the wrapping <html> element.

Any tips?

您正在寻找document.documentElement属性。

I guess for completeness' sake, I'll add another way to access it:

document.getElementsByTagName('html')[0];

Obviously this is a little more verbose, but it's always going to work regardless of the structure or standards-mode of your document.

You can also reach the HTML element with:

var html = document.body.parentNode;
alert(html.nodeName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM