实际上,我想使用querySelectorAll获得多个伪元素。
我阅读了一些相关文章,所有开发人员都说我们无法使用querySelectorAll来获取多个伪元素。
我们只有一个元素可以使用querySelector和类似下面的代码。
const textAfter = window.getComputedStyle(
document.querySelector(".text-after"), ":after"
).getPropertyValue("color")
console.log(textAfter)
但是有时候我们的项目目的需要使用多个选择器。所以,这就是为什么有人在这里经历过?谁可以帮助解决这个问题?
感谢@All
颠倒顺序。不用尝试计算元素列表的属性,而是为列表中的每个元素计算每个单个元素的属性:
const elements = Array.from(document.querySelectorAll(`.text-after`));
const allOfThem = elements.map(element =>
window.getComputedStyle(element, ":after").getPropertyValue("color")
);
console.log(allOfThem);
((我们在这里使用Array.from
,因为虽然.forEach
附带了HTMLCollection,但.map
却附带了[[not,因此在进行映射之前,我们必须将其强制成为真实数组)]] >