document.getElementByIdx_x
时间: 2024-06-20 12:04:10 浏览: 81
`document.getElementById()`方法用于返回对拥有指定 ID 的第一个对象的引用。
其中,`getElementById()`方法中的参数是要返回的元素的 ID,该参数是区分大小写的字符串。
例如,在以下 HTML 中,我们可以使用`document.getElementById()`方法来获取ID为"example"的元素。
```html
<!DOCTYPE html>
<html>
<head>
<title>getElementById Demo</title>
</head>
<body>
<div id="example">
<h1>This is an example</h1>
<p>Example paragraph.</p>
</div>
<script>
var example = document.getElementById("example");
console.log(example);
</script>
</body>
</html>
```
在上面这段代码中,我们使用了 `getElementById()`方法来获取 `example` 元素并将其赋值给 `example` 变量。 之后,我们使用`console.log()`方法在控制台中打印 `example` 变量以查看其内容。