--- title: HTML DOM getElementById Method localeTitle: HTML DOM getElementById方法 --- `getElementById()`方法返回具有指定值的id属性的元素。它需要一个参数,它是所需元素的id的区分大小写的字符串。 此方法是HTML DOM中最常用的方法之一,几乎每次您想要操作文档中的元素或从中获取信息时都会使用此方法。这是一个简单的语法示例: **HTML内容:** ```html
``` **JavaScript内容:** ```javascript document.getElementById("demo"); // Returns the element with id "demo" ``` 如果你有多个具有相同`id`值的元素(糟糕的做法!), `getElementById`将返回找到的第一个元素: ```html