gpt4 book ai didi

javascript - 检索 CSSStyleDeclaration 原生 Javascript 对象的键

转载 作者:行者123 更新时间:2023-12-01 00:13:48 26 4
gpt4 key购买 nike

我想检索可以设置为 dom 对象的所有可用键/样式的列表:CSSStyleDeclaration 对象。如果我在 chrome 中运行这行代码

console.log(Object.keys(document.querySelector('.class1').style));

我得到了预期的结果:

(517) ["alignContent", "alignItems", "alignSelf", "alignmentBaseline", "all", "animation", "animationDelay", "animationDirection", "animationDuration", "animationFillMode", "animationIterationCount", "animationName", "animationPlayState", "animationTimingFunction", "backdropFilter", "backfaceVisibility", "background", "backgroundAttachment", "backgroundBlendMode", "backgroundClip", "backgroundColor", "backgroundImage", "backgroundOrigin", "backgroundPosition", "backgroundPositionX", "backgroundPositionY", "backgroundRepeat", "backgroundRepeatX", "backgroundRepeatY", "backgroundSize", "baselineShift", "blockSize", "border", "borderBlockEnd", "borderBlockEndColor", "borderBlockEndStyle", "borderBlockEndWidth", "borderBlockStart", "borderBlockStartColor", "borderBlockStartStyle", "borderBlockStartWidth", "borderBottom", "borderBottomColor", "borderBottomLeftRadius", "borderBottomRightRadius", "borderBottomStyle", "borderBottomWidth", "borderCollapse", "borderColor", "borderImage", "borderImageOutset", "borderImageRepeat", "borderImageSlice", "borderImageSource", "borderImageWidth", "borderInlineEnd", "borderInlineEndColor", "borderInlineEndStyle", "borderInlineEndWidth", "borderInlineStart", "borderInlineStartColor", "borderInlineStartStyle", "borderInlineStartWidth", "borderLeft", "borderLeftColor", "borderLeftStyle", "borderLeftWidth", "borderRadius", "borderRight", "borderRightColor", "borderRightStyle", "borderRightWidth", "borderSpacing", "borderStyle", "borderTop", "borderTopColor", "borderTopLeftRadius", "borderTopRightRadius", "borderTopStyle", "borderTopWidth", "borderWidth", "bottom", "boxShadow", "boxSizing", "breakAfter", "breakBefore", "breakInside", "bufferedRendering", "captionSide", "caretColor", "clear", "clip", "clipPath", "clipRule", "color", "colorInterpolation", "colorInterpolationFilters", "colorRendering", "columnCount", "columnFill", …]

但是在 Firefox 中我得到一个空数组,而在 Edge 中我得到一个只有 1 个元素的数组

[object Array]: ["WebkitAnimation"]
0: "WebkitAnimation"
length: 1

我的一行代码有什么问题?

最佳答案

Window.getComputedStyle() 方法返回一个包含元素所有 CSS 属性值的对象。使用以下内容

let compStyles = window.getComputedStyle(document.querySelector('.class1'));
console.log(Object.values(compStyles));

关于javascript - 检索 CSSStyleDeclaration 原生 Javascript 对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59905726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com