面试题答案
一键面试实现思路
- 使用
document.querySelectorAll
方法获取所有具有类名item
的 DOM 元素,该方法会返回一个NodeList
。 - 遍历这个
NodeList
,对于每一个元素,使用element.textContent
属性来设置其文本内容为 '新内容'。
具体代码
// 获取所有类名为 'item' 的元素
const items = document.querySelectorAll('.item');
// 遍历并设置文本内容
items.forEach((item) => {
item.textContent = '新内容';
});