logo

实现 `getByClassName()` 与 `getByClassnameHierarchy()`:在 DOM 树中按 className 查找元素,并支持层级选择(例如 `a b c` 表示在 a 的后代里找 b 的后代里找 c)。

Implement `getByClassName()` and `getByClassnameHierarchy()` to search the DOM by class name, including hierarchical selectors like `a b c`.

题目类型: 技术面试题

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

难度: medium

标签: Dropbox, DOM, Traversal, Vanilla JS

目标岗位: Front End Developer, Front End Engineer, Web Developer

目标公司: Dropbox

参考答案摘要

答案(实现思路) Dropbox 前端常考“真实 DOM 操作”。核心是: 遍历 DOM + class 匹配 +(层级版) 逐层收缩候选集合 。 1) getByClassName(root, className) 从 root 开始 DFS/BFS 遍历所有元素节点。 使用 el.classList.contains(className) 做匹配。 返回所有匹配元素数组。 2) getByCl...

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

← 返回面试题库

实现 `getByClassName()` 与 `getByClassnameHierarchy()`:在 DOM 树中按 className 查找元素,并支持层级选择(例如 `a b c` 表示在 a 的后代里找 b 的后代里找 c)。

Mediumjavascriptdom

想查看完整答案?

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

前往学习中心查看答案