logo

解释 `document.querySelector()` 和 `document.getElementById()` 之间的区别

Explain the difference between `document.querySelector()` and `document.getElementById()`

题目类型: 技术面试题

这是一道技术面试题,常见于澳洲IT公司面试中。

难度: easy

分类: DOM Manipulation & Events

标签: web-api, javascript, html

参考答案摘要

TL;DR document.querySelector() 和 document.getElementById() 都是用于从 DOM 中选择元素的方法,但它们有关键的区别。 document.querySelector() 可以使用 CSS 选择器选择任何元素并返回第一个匹配项,而 document.getElementById() 通过其 ID 选择一个元素并返回具有该特定 ID 的元素。 ...

本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。

← 返回面试题库

解释 `document.querySelector()` 和 `document.getElementById()` 之间的区别

Easyjavascripthtml-css

想查看完整答案?

登录匠人学院学习中心,获取 STAR 格式回答和详细技术解析

前往学习中心查看答案