在 JavaScript 中,你可以使用 'window.getComputedStyle' 方法来获取正在生效的类名。

首先,你需要获取要查询的元素。你可以通过 'document.querySelector' 或 'document.getElementById' 方法来获取元素。假设你要获取 'body' 元素:

const body = document.querySelector('body');

然后,你可以使用 'window.getComputedStyle' 方法来获取正在生效的类名。这个方法会返回一个包含元素的所有样式属性和值的对象。你可以通过访问 'classList' 属性来获取元素的类名列表。

const computedStyle = window.getComputedStyle(body);
const classNames = Array.from(computedStyle.classList);

现在,'classNames' 变量将包含 'body' 元素的所有正在生效的类名。

注意,'classList' 属性是一个只读属性,返回一个只读的 DOMTokenList 对象。如果你想要对类名进行操作,你可以使用它的方法,如 'add'、'remove' 和 'toggle'。


原文地址: https://www.cveoy.top/t/topic/paju 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录