【问题标题】:How get all style of element [duplicate]如何获得元素的所有样式[重复]
【发布时间】:2021-12-10 14:24:25
【问题描述】:

我有这样的风格:

.filter {
      background-image: url("../images/index.png"); !important;
      background-blend-mode: difference !important;
      filter: invert(1);
      box-shadow: inset 0 0 0 1px black;
      **other style**
    }

我想获得所有可能的风格?

如果我想得到类似的属性名

$(selector).css(property-name:property-value);

我应该为它写很多行代码,因为样式很多。

  • 我无法使用内联样式并通过 attr 获取它

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    你可以使用getComputerStyle

    const h3 = document.querySelector("h3");
    const result = document.getComputedStyle(h3).content();
    

    GetComputedStyle Docs

    您也可以将getAttribute 方法与style 一起使用

    conat h3 = document.querySelector("h3");
    
    let styles = h3.getAttribute("style");
    styles.replace(" ", "")
        .split(";")
        .map(s => s.split(":"));
    

    【讨论】:

      【解决方案2】:

      您无法获取单个 CSS 类的属性,但可以获取元素的所有计算样式:

      var e = getElementById('myElement');
      console.log( getComputedStyle(e) );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 2014-11-22
        • 2021-10-16
        • 1970-01-01
        • 2014-01-30
        • 2018-01-22
        • 2021-01-19
        相关资源
        最近更新 更多