Javascript 正文没有 getElementById 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11235409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No getElementById for body?
提问by u283863
This question has disturbed me for a long time. Sorry if it is a stupid question.
这个问题困扰了我很久。对不起,如果这是一个愚蠢的问题。
Before, I have known that you can get the elements with a class name
之前我就知道可以通过类名来获取元素
document.body.getElementsByClassName("foo");
And I was so lazy, so I just copied and pasted the code to the other part to do this
而且我太懒了,所以我只是将代码复制粘贴到另一部分来做到这一点
document.body.getElementById("bar");
I accidentally found it won't work. I tested and it says
我不小心发现它不起作用。我测试过,它说
TypeError: Object #<HTMLBodyElement> has no method 'getElementById'
类型错误:对象 #<HTMLBodyElement> 没有方法“getElementById”
So why does it have getElementsByClassName
andgetElementsByTagName
and all those similar methods, but only no getElementById
?
那么为什么它有getElementsByClassName
andgetElementsByTagName
和所有这些类似的方法,但只有没有getElementById
?
typeof document === typeof document.body //true
Their types are the same, so they should have the same thing. But it does not seem to be the case here.
它们的类型相同,因此它们应该具有相同的内容。但这里似乎并非如此。
采纳答案by gdoron is supporting Monica
You can have multiple elements with the same class name so narrowing down the search to start with a specific node make sense.
您可以拥有多个具有相同类名的元素,因此缩小搜索范围以从特定节点开始是有意义的。
It doesn't make sense with id because it it should be unique.
id 没有意义,因为它应该是唯一的。
You can have only one id
in the document
, this why getElementById
is a method of document
.
你只能有一个id
在document
,这是为什么呢getElementById
是一个方法document
。
Example:
例子:
<body>
<div id="start">
<span class="a">
</div>
<div class="a">
</div>
</body>
Start searching for class a
from the node <div id="start">
will give you one element,
While if you would have start from the top node- document, it would have ended with two elements.
a
从节点开始搜索 class<div id="start">
将为您提供一个元素,
而如果您从顶部节点文档开始,它将以两个元素结束。
Regarding to the typeof
comparing:
关于typeof
对比:
typeof 1 == typeof 2 == "Number" // true
1 !== 2 // true.
typeof
only checks for the type, not the value, document
and document.body
are both objects, but different objects.
typeof
仅检查的类型,而不是值,document
和document.body
是两个对象,但不同的对象。
typeof document === typeof document.body === typeof null === "object" // true
document === document.body // false!!!
As you can see, null
and document
share the same type, but do they have the same methods...? NO
如您所见,null
并document
共享相同的类型,但它们是否具有相同的方法......?不
回答by jsoverson
Ids are unique to the entire document, therefore it makes no sense to scope them to children nodes of the document.
Id 对于整个文档是唯一的,因此将它们的范围限定到文档的子节点是没有意义的。
Class names are not unique and there are use cases that make sense to find elements that have classnames below another element.
类名不是唯一的,并且有一些用例可以找到在另一个元素下具有类名的元素。
body.getElementsByClassName('foo')
will get the elements that have classname 'foo' but are contained within the body.
body.getElementsByClassName('foo')
将获取具有类名 'foo' 但包含在主体中的元素。
document.getElementsByClassName('foo')
will get all elements with classname 'foo' in the entire document, including the <head>
.
document.getElementsByClassName('foo')
将获取整个文档中所有类名为 'foo' 的元素,包括<head>
.
回答by chaos
typeof document
and typeof document.body
are the same because they're both object
. Types don't work the way you think they do in JS with regard to objects. So no, they're not the same, and there's no particular reason they would have to support the same function set. (Even objects with the same prototype don't have to support the same function set, but that's another matter.) Just call getElementById
on document
and it will work.
typeof document
并且typeof document.body
是相同的,因为它们都是object
. 对于对象,类型与您在 JS 中认为的方式不同。所以不,它们不一样,并且没有特别的理由它们必须支持相同的功能集。(即使是相同的原型对象没有支持相同的功能集,但那是另一回事。)只是叫getElementById
上document
,它会工作。
("Doctor, doctor, it hurts when I hold my arm over my head and rotate it rapidly through a figure-8 pattern!" "Yeah? So knock it off.")
(“医生,医生,当我把手臂举过头顶并快速旋转成 8 字形时会痛!”“是吗?所以把它关掉。”)
回答by JonWarnerNet
Your typeof
example is not comparing that they are the same thing but that they are the same 'type', for which both return object
:
您的typeof
示例不是比较它们是同一件事,而是比较它们是相同的“类型”,两者都返回object
:
Output from Firebug console:
Firebug 控制台的输出:
>>> typeof document
"object"
>>> typeof document.body
"object"
Further details on typeof
can be found at:
有关更多详细信息,请typeof
访问:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof
https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof