JavaScript 获取样式

发布于 2024-10-01 18:03:58 字数 365 浏览 1 评论 0原文

是否可以使用 JavaScript 获取对象的所有样式?像这样的东西:


main.css
-------
#myLayer {
  position: absolute;
  width: 200px;
  height: 100px;
  color: #0000ff;
}

main.js
-------
var ob = document.getElementById("myLayer");
var pos = ob.(getPosition);
// Pos should equal "absolute" but
// ob.style.position would equal null
// any way to get absolute?


Is it possible to get ALL of the styles for an object using JavaScript? Something like:


main.css
-------
#myLayer {
  position: absolute;
  width: 200px;
  height: 100px;
  color: #0000ff;
}

main.js
-------
var ob = document.getElementById("myLayer");
var pos = ob.(getPosition);
// Pos should equal "absolute" but
// ob.style.position would equal null
// any way to get absolute?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2024-10-08 18:03:58

您正在谈论所谓的计算样式,请查看以下文章以了解如何获取它:

从上一篇文章来看,这里有一个函数:

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

如何使用它:

CSS:

/* Element CSS*/
div#container{
    font: 2em/2.25em Verdana, Geneva, Arial, Helvetica, sans-serif;
}

JS:

var elementFontSize = getStyle(document.getElementById("container"), "font-size");

You are talking about what is known as Computed Style, check out these article on how to get it:

From the last article, here is a function:

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

How to use it:

CSS:

/* Element CSS*/
div#container{
    font: 2em/2.25em Verdana, Geneva, Arial, Helvetica, sans-serif;
}

JS:

var elementFontSize = getStyle(document.getElementById("container"), "font-size");
青巷忧颜 2024-10-08 18:03:58

您可能会使用:

var ob = document.getElementById("myLayer");
var pos = ob.style.position;

每个 CSS 属性都有它自己的对象模型。通常那些包含'-'的css属性是使用java模型编写的。

例如:

//getting background-color property
var ob = document.getElementById("myLayer");
var color = ob.style.backgroundColor;

如果您想获取为某个对象定义的所有 css 属性,则必须将它们一一列出,因为即使您没有在样式表中设置该属性,对象也会将其与默认值。

You might use:

var ob = document.getElementById("myLayer");
var pos = ob.style.position;

Every CSS property has it's own object model. Usually those css properties that contain '-' are written using java model.

For example:

//getting background-color property
var ob = document.getElementById("myLayer");
var color = ob.style.backgroundColor;

If you want to get all the css properties that are defined for an object, you will have to list them one by one, because even if you did not set the property in your style sheet, an object will have it with the default value.

潜移默化 2024-10-08 18:03:58

Polyfill 使用 javascript 获取元素的当前 CSS 样式...访问 链接 了解更多信息

/**
* @desc : polyfill for getting the current CSS style of the element
* @param : elem - The Element for which to get the computed style.
* @param : prop - The Property for which to get the value
* @returns : The returned style value of the element
**/
var getCurrentStyle = function (ele, prop) {

var currStyle;
if (!window.getComputedStyle) {
    currStyle = ele.currentStyle[prop];
} else {
    currStyle = window.getComputedStyle(ele).getPropertyValue(prop);
}

return currStyle;
}


/** How to use **/
var element = document.getElementById("myElement");
getCurrentStyle(element, "width"); // returns the width value   

Polyfill to get the current CSS style of element using javascript ... Visit the link for more info

/**
* @desc : polyfill for getting the current CSS style of the element
* @param : elem - The Element for which to get the computed style.
* @param : prop - The Property for which to get the value
* @returns : The returned style value of the element
**/
var getCurrentStyle = function (ele, prop) {

var currStyle;
if (!window.getComputedStyle) {
    currStyle = ele.currentStyle[prop];
} else {
    currStyle = window.getComputedStyle(ele).getPropertyValue(prop);
}

return currStyle;
}


/** How to use **/
var element = document.getElementById("myElement");
getCurrentStyle(element, "width"); // returns the width value   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文