当HTML DOM中存在重复ID的元素时,javascript getElementById()的行为? [英] behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM?

查看:393
本文介绍了当HTML DOM中存在重复ID的元素时,javascript getElementById()的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当HTML DOM中存在重复ID的元素时,有人可以了解javascript getElementById()的行为吗?

Can someone shed some light on the behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM??

推荐答案

如果您有多个具有相同ID的元素,则可以使用

If you have multiple elements with the same id, you can select them all with

document.querySelectorAll("[id='myid']")

但是,如果你控制它,你应该修复HTML。

However, if you are in control of it you should just fix the HTML.

这篇关于当HTML DOM中存在重复ID的元素时,javascript getElementById()的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆