链接 getElementById
我一直在寻找这个问题的答案,但找不到答案,所以我想尝试一下 StackOverflow。
在 javascript 中,这是否有效:
x = document.getElementById('myId'); y = x.getElementById('mySecondId');
我知道这可以通过 getElementsByTagName
完成,但我不确定 getElementById
返回的对象是否是能够使用 getElementById
方法。
我知道每个文档的 ID 应该是唯一的,但有时情况并非如此。
谢谢!
I've been looking for an answer to this question but I could find none so I thought I'd try StackOverflow.
In javascript, is this valid:
x = document.getElementById('myId');
y = x.getElementById('mySecondId');
I know this can be done with getElementsByTagName
but I'm not sure if the object returned by getElementById
is able to use the getElementById
method.
I know that the ID is supposed to be unique per document, but sometimes this is just not the case.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有。
...但是您可以:
在此页面上尝试一下:
编辑:正如 Pumbaa80 指出的那样,您想要其他东西。嗯,就在这里。谨慎使用。
示例: http://jsfiddle.net/3xTcX/
Nope.
...But you can, though:
Try it on this page:
Edit: As Pumbaa80 pointed out, you wanted something else. Well, here it is. Use with caution.
An example: http://jsfiddle.net/3xTcX/
嗯,找出答案的最好方法就是尝试一下。在这种情况下,它不起作用,因为
getElementById
方法仅适用于DOMDocument
对象(例如document
变量),而不适用于DOMElement
对象,它们是单独的节点。我认为它也应该在这些上可用,但是嘿,我不同意 DOM API 的大部分设计......Well, the best way to find out is to try it. In this case, it won't work, since the
getElementById
method is only available onDOMDocument
objects (e.g. thedocument
variable) and not onDOMElement
objects, which are individual nodes. I think it should have been available on those also, but hey, I disagree with most of the design of the DOM APIs...您可以在示例中使用
y = x.querySelector('#'+'mySecondId');
而不是y = x.getElementById('mySecondId');
的问题。Element.getElementById
不存在,但您可以按照其他答案中提到的方式添加它,即使不建议向Element
添加方法。如果您绝对想使用这种解决方案,以下是一种可能性:在
Element.prototype 内使用
的优点是,即使元素尚未在 DOM 中(例如使用element.querySelector
而不是document.getElementById
的一个优点。 getElementByIddocument.createElement
创建元素之后),element.querySelector
也能正常工作。You can just use
y = x.querySelector('#'+'mySecondId');
instead ofy = x.getElementById('mySecondId');
in the sample of the question.Element.getElementById
doesn't exist but you can add it as mentioned in other answers, even if it is not recommended to add a method toElement
. In case you want absolutely use this kind of solution, below is a possibility:One advantage to use
element.querySelector
instead ofdocument.getElementById
insideElement.prototype.getElementById
is thatelement.querySelector
is working even if the element is not already in the DOM, such as after creating it withdocument.createElement
.没有。
默认情况下,只有
document
对象具有getElementById
方法。即使
x
是 iframe 或其他内容,在访问另一个getElementById
之前,您仍然必须访问一些其他属性或其他内容。Nope.
Only the
document
object has the methodgetElementById
by default.Even if
x
was an iframe or something, you'd still have to access some other properties or whatever before you got to anothergetElementById
.当有多个 id 时,请考虑不使用 id
也许类或自定义属性更好,然后您可以使用
document.querySelectorAll
来查找它们。Consider not using id when there are more than one
Maybe a class or custom attribute is better, you can then use
document.querySelectorAll
to fins them.这是基于 Node.contains 的快速替代方案
最后一行(在最新的 Chrome 和 Firefox 上进行了分析)的执行速度比 jQuery 的等效项快 4 到 10 倍
唯一好的替代方案是
querySelector
(更快一点)This is a fast alternative based on Node.contains
The last line (profiled on latest Chrome and Firefox) performs 4x to 10x faster than jQuery's equivalent
The only good alternative is
querySelector
(a bit faster)