【问题标题】:How to get all the applied styles of an element by just giving its id?如何仅通过提供元素的 id 来获取元素的所有应用样式?
【发布时间】:2021-09-02 22:11:47
【问题描述】:

我正在尝试编写一个函数,该函数采用元素的 Id 并给出应用于该元素的所有样式属性(及其值)的列表。它应该考虑内联样式以及css文件中定义的样式。

当我在参数中提供样式属性名称以及元素的 id 时,我可以使函数工作,但我只想传递元素的 id,并且应该能够获取所有样式属性以及值。

函数应该类似于getStyleById(elementId);

PFB 代码 sn-p 到目前为止:

var styleNode = [];
var styles;
var sty = x.style;
    
var len = sty.length;
    
for (var i = 0; i < len; i++) 
{
    styles = sty.item(i);
       
    if (x.currentStyle)     //IE for External/Global Styles
    {
        var a = x.currentStyle[styles];
            
        styleNode.push(styles + ":" + a);
    }
    else if (document.defaultView && document.defaultView.getComputedStyle)    //Firefox,Chrome,Safari for External/Global Styles
    {
        var b = document.defaultView.getComputedStyle(x, "").getPropertyValue(styles);
          
        styleNode.push(styles + ":" + b);
    }
    else           //Works in Inline Styles only
    {
        var c = x.style[styles];
          
        styleNode.push(styles + ":" + c);
    }
}

【问题讨论】:

  • 我认为你没有提到什么不起作用
  • 检查this answer
  • 也许这会有所帮助 - stackoverflow.com/questions/5509830/…
  • @LiviuT。当我在参数中提供样式属性名称以及元素的 id 时,我可以使函数工作,但我只想传递元素的 id 并且应该能够获取所有样式属性以及值。

标签: javascript html css


【解决方案1】:

使用以下方法:

  • 循环遍历CSSStyleDeclaration 对象(getComputedStyle) 的索引以获取每个已知的属性名称。使用getPropertyValue + 这个名字来获取值。
    代码优化:每次迭代不要使用getComputedStyle,而是将其存储在循环外的变量中。
  • currentStyle 使用普通的for ( name in object ) 循环。
  • 对内联样式使用相同的循环方法

代码:

function getStyleById(id) {
    return getAllStyles(document.getElementById(id));
}
function getAllStyles(elem) {
    if (!elem) return []; // Element does not exist, empty list.
    var win = document.defaultView || window, style, styleNode = [];
    if (win.getComputedStyle) { /* Modern browsers */
        style = win.getComputedStyle(elem, '');
        for (var i=0; i<style.length; i++) {
            styleNode.push( style[i] + ':' + style.getPropertyValue(style[i]) );
            //               ^name ^           ^ value ^
        }
    } else if (elem.currentStyle) { /* IE */
        style = elem.currentStyle;
        for (var name in style) {
            styleNode.push( name + ':' + style[name] );
        }
    } else { /* Ancient browser..*/
        style = elem.style;
        for (var i=0; i<style.length; i++) {
            styleNode.push( style[i] + ':' + style[style[i]] );
        }
    }
    return styleNode;
}

【讨论】:

  • 非常感谢 :) ...效果很好...但唯一的问题是我只为元素提供了 2 个 CSS 样式属性,而此函数返回的值包含很多 CSS 属性.就像在这个元素中

    FirstId

    ,而函数的返回值包含....... .............................背景附件:滚动,背景剪辑:边框、背景颜色:RGB(170、172、204)、背景图像:无、背景原点:填充框等......
  • @manishekhawat 你想only 得到inline 样式(没有样式表样式)吗?那么你应该只使用else块中的代码:function getAllStyles(elem){if(!elem)return [];for(var styleNode=[],style=elem.style,i=0;i&lt;style.length;i++)styleNode.push(style[i]+':'+style.getPropertyValue(style[i]));return styleNode}
  • 这段代码太棒了!有没有办法只获取样式表或特定样式表应用的样式?类似于 chrome 的开发者工具 css 样式分解。
  • 遍历所有样式样式表并使用matches/matchesSelector测试每个选择器。实现是不平凡的,值得一个自己的问题。特别是如果你想知道哪种风格是活跃的。
  • 最后一行代码中存在错误:style[style[i]] 将返回“undefined”,因为 style[i] 返回“background-image”但它必须是“backgroundImage”而是!
【解决方案2】:

要获得应用的样式:使用document.querySelector('#notion-app').getAttribute('style')
这将作为字符串返回:"width: 1280px; max-width: 1280px; align-self: center; margin-top: 1px; margin-bottom: 1px;".
您可以使用.split(';') 进一步将其分解为数组。


获取计算样式(最终应用的样式):
window.getComputedStyle(document.querySelector('#notion-app'))).cssText

【讨论】:

  • 不完全清楚我是否可以将其应用于具有 id 的特定元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 2021-12-21
  • 1970-01-01
  • 2011-12-10
相关资源
最近更新 更多