javascript 为什么 .getPropertyValue() 不会返回“borderRadius”属性的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10803023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't .getPropertyValue() return a value for the "borderRadius" property?
提问by WK_of_Angmar
Here is the function:
这是函数:
function lastmenuborder() {
var articleparent = document.getElementById('article').parentNode;
var articlestyle = window.getComputedStyle(articleparent,null).getPropertyValue('borderRadius');
alert (articlestyle);
}
I get no value, yet the css for the parent node is:
我没有任何价值,但父节点的 css 是:
div#mainbody div.placeholder {
border-radius: 3px;
}
What would I have to change to return "3px"? All help greatly appreciated; I am still a newb at JavaScript.
我必须改变什么才能返回“3px”?非常感谢所有帮助;我仍然是 JavaScript 的新手。
回答by
For getPropertyValue()
, you use hyphens instead of camelCase.
对于getPropertyValue()
,您使用连字符而不是驼峰命名。
This works in Chrome...
这适用于 Chrome...
.getPropertyValue('border-radius');
But Firefox seems to require specific corners using this syntax...
但是 Firefox 似乎需要使用这种语法的特定角落......
.getPropertyValue('border-top-left-radius');
回答by GianFS
getComputedStyleis not supported on IE8 below, to fix this use:
下面的 IE8 不支持getComputedStyle,以修复此使用:
if (!window.getComputedStyle) {
window.getComputedStyle = function(el, pseudo) {
this.el = el;
this.getPropertyValue = function(prop) {
var re = /(\-([a-z]){1})/g;
if (prop == 'float') prop = 'styleFloat';
if (re.test(prop)) {
prop = prop.replace(re, function () {
return arguments[2].toUpperCase();
});
}
return el.currentStyle[prop] ? el.currentStyle[prop] : null;
}
return this;
}
}
var elem = window.getComputedStyle(document.getElementById('test'), "");
alert(elem.getPropertyValue("borderRadius"));