[英]Is there a way to call on the HTML element without using tagname?
This is a simple question: Is there a way to call on the HTML element without using: 这是一个简单的问题:是否可以不使用以下方法调用HTML元素:
document.getElementsByTagName('HTML')[0]
using JavaScript. 使用JavaScript。 For getting the body you can do:
为了获得身体,您可以:
document.body
Is there a simpler way to do this with JavaScript? 有没有更简单的方法可以用JavaScript做到这一点?
All of these can be used to get a reference to the <html>
element: 所有这些都可以用来获取对
<html>
元素的引用:
document.documentElement
document.querySelector('html')
document.querySelectorAll('html')[0]
document.getElementById('foo')
- if <html id="foo">
is set document.getElementById('foo')
-如果设置了<html id="foo">
document.getElementsByClassName('bar')[0]
- if <html class="bar">
is set document.getElementsByClassName('bar')[0]
-如果设置了<html class="bar">
document.getElementsByTagName('html')[0]
document.head.parentElement
document.body.parentElement
You can also get a reference to the root <html>
element by navigating upwards using parentElement
or parentNode
: 您还可以通过使用
parentElement
或parentNode
向上导航来获取对根<html>
元素的引用:
var anyElement = document.getElementById('somethingOtherThanHtml');
var htmlElement = anyElement;
while( htmlElement.tagName != "HTML" ) {
htmlElement = htmlElement.parentElement;
}
And document
is also a member of window
(in fact, window
is the "root object" in all browser web-page scripting scenarios), so if you only have window
you can go window.document.documentElement
and so on. 而且
document
也是window
的成员(实际上, window
是所有浏览器网页脚本编写方案中的“根对象”),因此,如果只有window
,则可以进入window.document.documentElement
等。
您可以参考document.documentElement
:
console.log(document.getElementsByTagName('HTML')[0] === document.documentElement);
您可以将ID添加到HTML元素,然后使用document.getElementById(“ idOfTheElement”)
You can use something like document.getElementByClassName("classname")
. 您可以使用诸如
document.getElementByClassName("classname")
东西。
classname represents the class for that tag classname代表该标签的类
您可以使用queryselector:
document.querySelector('HTML')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.