在IE中将计算出的背景颜色作为rgb [英] get computed background color as rgb in IE

查看:87
本文介绍了在IE中将计算出的背景颜色作为rgb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码在IE中获取RGB背景颜色:

I am trying to get the RGB background color in IE using the following code:

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;
    }
}

var $b = $("<button>");
$b.css("backgroundColor", "ButtonFace");
$("body").append($b);
alert("button bg color is: "+ getStyle($b[0],"backgroundColor"));
//alerts 'buttonface'

这不会像firefox一样返回rgb颜色值,它返回'buttonface',这对我没用。

this does not return an rgb color value like firefox does, it returns 'buttonface' which is useless to me.

推荐答案

我一直致力于跨浏览器实现 getStyle函数,
我的函数尚未完成,但我可以帮助您解决IE中的这个特定问题。

I have been working on a cross-browser implementation of a "getStyle" function, my function isn't complete yet but I can help you to solve this specific problem you have with IE.

对于计算出的 backgroundColor ,我正在使用这个页面,它使用IE特定的 queryCommandValue 获取 BackColor

For the computed backgroundColor, I'm using a hack proposed in this page, it uses the IE specific queryCommandValue method to get the BackColor of a selection.

关于你发布的实现,我建议检查一下rst如果标准的 getComputedStyle 方法存在 document.defaultView ,因为某些浏览器如Opera,提供IE特定的 currentStyle 兼容性对象。

About the implementation you post, I would recommend to check first if the standard getComputedStyle method via the document.defaultView exists, because some browsers like Opera, provide the IE specific currentStyle object for compatibility.

所以我重构了你的函数并包含了 IE hack

So I've refactored your function and included the IE hack:

function getStyle(elem, name) {
  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 if (elem.currentStyle) {
    if (/backgroundcolor/i.test(name)) {
      return (function (el) { // get a rgb based color on IE
        var oRG=document.body.createTextRange();
        oRG.moveToElementText(el);
        var iClr=oRG.queryCommandValue("BackColor");
          return "rgb("+(iClr & 0xFF)+","+((iClr & 0xFF00)>>8)+","+
                      ((iClr & 0xFF0000)>>16)+")";
      })(elem);
    }

    return elem.currentStyle[name];
  } else if (elem.style[name]) {
    return elem.style[name];
  } else  {
    return null;
  }
}

希望很快我会发布一个更通用的实现,但这足以解决你的 backgorundColor 问题。

Hopefully soon I'll post a more generic implementation, but this will be enough to solve your backgorundColor issue.

你可以测试上面的函数这里

You can test the above function here.

这篇关于在IE中将计算出的背景颜色作为rgb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆