链接 getElementById

发布于 2024-11-02 01:20:21 字数 359 浏览 0 评论 0原文

我一直在寻找这个问题的答案,但找不到答案,所以我想尝试一下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁吢 2024-11-09 01:20:21

没有。

...但是您可以:

Element.prototype.getElementById = function(id) {
    return document.getElementById(id);
}

在此页面上尝试一下:

var x = document.getElementById('footer').getElementById('copyright');

编辑:正如 Pumbaa80 指出的那样,您想要其他东西。嗯,就在这里。谨慎使用。

Element.prototype.getElementById = function(req) {
    var elem = this, children = elem.childNodes, i, len, id;

    for (i = 0, len = children.length; i < len; i++) {
        elem = children[i];

        //we only want real elements
        if (elem.nodeType !== 1 )
            continue;

        id = elem.id || elem.getAttribute('id');

        if (id === req) {
            return elem;
        }
        //recursion ftw
        //find the correct element (or nothing) within the child node
        id = elem.getElementById(req);

        if (id)
            return id;
    }
    //no match found, return null
    return null;
}

示例: http://jsfiddle.net/3xTcX/

Nope.

...But you can, though:

Element.prototype.getElementById = function(id) {
    return document.getElementById(id);
}

Try it on this page:

var x = document.getElementById('footer').getElementById('copyright');

Edit: As Pumbaa80 pointed out, you wanted something else. Well, here it is. Use with caution.

Element.prototype.getElementById = function(req) {
    var elem = this, children = elem.childNodes, i, len, id;

    for (i = 0, len = children.length; i < len; i++) {
        elem = children[i];

        //we only want real elements
        if (elem.nodeType !== 1 )
            continue;

        id = elem.id || elem.getAttribute('id');

        if (id === req) {
            return elem;
        }
        //recursion ftw
        //find the correct element (or nothing) within the child node
        id = elem.getElementById(req);

        if (id)
            return id;
    }
    //no match found, return null
    return null;
}

An example: http://jsfiddle.net/3xTcX/

只等公子 2024-11-09 01:20:21

嗯,找出答案的最好方法就是尝试一下。在这种情况下,它不起作用,因为 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 on DOMDocument objects (e.g. the document variable) and not on DOMElement 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...

百合的盛世恋 2024-11-09 01:20:21

您可以在示例中使用 y = x.querySelector('#'+'mySecondId'); 而不是 y = x.getElementById('mySecondId');的问题。

Element.getElementById 不存在,但您可以按照其他答案中提到的方式添加它,即使不建议向 Element 添加方法。如果您绝对想使用这种解决方案,以下是一种可能性:

Element.prototype.getElementById = function(id) {
return this.querySelector("#"+id);
}

Element.prototype 内使用 element.querySelector 而不是 document.getElementById 的一个优点。 getElementById 的优点是,即使元素尚未在 DOM 中(例如使用 document.createElement 创建元素之后),element.querySelector 也能正常工作。

You can just use y = x.querySelector('#'+'mySecondId'); instead of y = 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 to Element. In case you want absolutely use this kind of solution, below is a possibility:

Element.prototype.getElementById = function(id) {
return this.querySelector("#"+id);
}

One advantage to use element.querySelector instead of document.getElementById inside Element.prototype.getElementById is that element.querySelector is working even if the element is not already in the DOM, such as after creating it with document.createElement.

时光清浅 2024-11-09 01:20:21

没有。

默认情况下,只有 document 对象具有 getElementById 方法。

即使 x 是 iframe 或其他内容,在访问另一个 getElementById 之前,您仍然必须访问一些其他属性或其他内容。

Nope.

Only the document object has the method getElementById 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 another getElementById.

帅的被狗咬 2024-11-09 01:20:21

当有多个 id 时,请考虑不使用 id

也许类或自定义属性更好,然后您可以使用 document.querySelectorAll 来查找它们。

els = document.querySelectorAll('[custom-attr]')

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.

els = document.querySelectorAll('[custom-attr]')
不必在意 2024-11-09 01:20:21

这是基于 Node.contains 的快速替代方案

var getById = function(id, context) {
  var el = document.getElementById(id);
  return context.contains(el) ? el : null;
}

var container = document.getElementById('container-element');
getById('my-element-id', container);

最后一行(在最新的 Chrome 和 Firefox 上进行了分析)的执行速度比 jQuery 的等效项快 4 到 10 倍

$('#my-element-id', container);

唯一好的替代方案是 querySelector (更快一点)

container.querySelector('#my-element-id');

This is a fast alternative based on Node.contains

var getById = function(id, context) {
  var el = document.getElementById(id);
  return context.contains(el) ? el : null;
}

var container = document.getElementById('container-element');
getById('my-element-id', container);

The last line (profiled on latest Chrome and Firefox) performs 4x to 10x faster than jQuery's equivalent

$('#my-element-id', container);

The only good alternative is querySelector (a bit faster)

container.querySelector('#my-element-id');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文