简体   繁体   English

在JavaScript中获取元素样式表样式

[英]Get Element StyleSheet Style in JavaScript

I've been using John Resig's getStyle function from Pro JavaScript Techniques to get the style of elements: 我一直在使用John Resig的Pro JavaScript技术的getStyle函数来获得元素的风格:

function getStyle(elem, name) {
    // J/S Pro Techniques p136
    if (elem.style[name]) {
        return elem.style[name];
    } else if (elem.currentStyle) {
        return elem.currentStyle[name];
    }
    else if (document.defaultView && document.defaultView.getComputedStyle) {
        name = name.replace(/([A-Z])/g, "-$1");
        name = name.toLowerCase();
        s = document.defaultView.getComputedStyle(elem, "");
        return s && s.getPropertyValue(name);
    } else {
        return null;
    }
}

However this method returns default styles for an element if no style is specified: 但是,如果未指定样式,则此方法返回元素的默认样式:

http://johnboxall.github.com/test/getStyle.html http://johnboxall.github.com/test/getStyle.html

alt text http://img.skitch.com/20081227-8qhxie51py21yxuq7scy32635a.png 替代文字http://img.skitch.com/20081227-8qhxie51py21yxuq7scy32635a.png

Is it possible to get only the stylesheet specified styles of an element (and return null if the style is undefined)? 是否可以只获取元素的样式表指定样式(如果未定义样式,则返回null)?

Update: 更新:

Why do I need such a beast? 为什么我需要这样的野兽? I'm building a small component that allows users to style elements. 我正在构建一个允许用户设置元素样式的小组件。 One of the styles that can be applied is text-align - left , center , right - Using getStyle unstyled elements default to center . 其中一种可以应用的样式是text-align - leftcenterright - 使用getStyle样式元素默认为center This makes it impossible to tell whether the element is centered because the user wanted it to be centered or is centered because that's the default style. 这使得无法判断元素是否居中,因为用户希望它居中或居中,因为这是默认样式。

Is it possible to get only the stylesheet specified styles of an element (and return null if the style is undefined)? 是否可以只获取元素的样式表指定样式(如果未定义样式,则返回null)?

That's effectively what is done by the routine you present. 这实际上是你所呈现的例程所做的。 The problem is, in most scenarios, most styles are not undefined - they're inherited and/or defined by the individual browser's internal stylesheet. 问题是,在大多数情况下,大多数样式都没有未定义 - 它们是由各个浏览器的内部样式表继承和/或定义的。

You could, with a whole lot of effort, iterate through all of the rules defining the style in question, in all of the stylesheets in the current view of the document, evaluate them for the element in question, and if none applied... and if none applied to a parent (or this particular style is not inherited)... then consider it undefined. 您可以通过大量的努力,迭代定义相关样式的所有规则,在文档的当前视图中的所有样式表中,为相关元素评估它们,如果没有应用...如果没有应用于父级(或者不继承此特定样式)...则认为它未定义。 This would be slow, and incredibly error-prone. 这将是缓慢的,并且非常容易出错。 I would not recommend trying it. 我不建议尝试它。

Perhaps you would do better to step back and ask why you would ever need such a thing? 也许你会更好地退后一步,问为什么你会需要这样的东西?

Maybe your component can wrap the styles that it controls? 也许你的组件可以包装它控制的样式? Then when a style is set through the component the component knows what the user wants. 然后,当通过组件设置样式时,组件知道用户想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM