在 IE 中获取计算出的背景颜色为 rgb
我试图使用以下代码在 IE 中获取 RGB 背景颜色:
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”。
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'
this does not return an rgb color value like firefox does, it returns 'buttonface' which is useless to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直致力于“getStyle”函数的跨浏览器实现,
我的功能尚未完成,但我可以帮助您解决 IE 遇到的具体问题。
对于计算的
backgroundColor
,我使用了此页面,它使用 IE 特定的queryCommandValue
方法获取 所选内容的BackColor
。关于您发布的实现,我建议首先检查通过
document.defaultView
的标准getCompulatedStyle
方法是否存在,因为某些浏览器(如 Opera)提供 IE 特定的currentStyle
对象以实现兼容性。因此,我重构了您的函数并包含了 IE hack:
希望很快我会发布一个更通用的实现,但这足以解决您的
backgorundColor
问题。您可以在此处测试上述函数。
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.
For the computed
backgroundColor
, I'm using a hack proposed in this page, it uses the IE specificqueryCommandValue
method to get theBackColor
of a selection.About the implementation you post, I would recommend to check first if the standard
getComputedStyle
method via thedocument.defaultView
exists, because some browsers like Opera, provide the IE specificcurrentStyle
object for compatibility.So I've refactored your function and included the IE hack:
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.