Skip to content

[js] 第277天 页面上的DOM有多个相同的ID,用js获取时结果会是怎么样的? #1790

Open
@haizhilin2013

Description

@haizhilin2013

第277天 页面上的DOM有多个相同的ID,用js获取时结果会是怎么样的?

我也要出题

Activity

liwudi

liwudi commented on Jan 17, 2020

@liwudi

只能获取第一个id的dom。

如果想要全部获取,需要先获取dom,在查看dom的属性id,再找出id属性值相同的这些dom。

zishiluojin

zishiluojin commented on Jan 18, 2020

@zishiluojin

只能获取第一个id的dom。

如果想要全部获取,需要先获取dom,在查看dom的属性id,再找出id属性值相同的这些dom。

想要获取全部id相同的dom可以用document.querySelectorAll()

xpsilvester

xpsilvester commented on Jan 19, 2020

@xpsilvester

document.getElementById() 方法获取到第一个id的dom,document.querySelectorAll()方法获取到全部id相同的dom

xiaoqiangz

xiaoqiangz commented on Aug 30, 2022

@xiaoqiangz

getElementById()只会获取到第一个dom,如果想要获取全部的可以使用querySelectorAll()。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@xpsilvester@xiaoqiangz@liwudi@zishiluojin

        Issue actions

          [js] 第277天 页面上的DOM有多个相同的ID,用js获取时结果会是怎么样的? · Issue #1790 · haizlin/fe-interview