为什么.getPropertyValue()不返回“borderRadius”属性的值?

时间:2012-05-29 16:22:33

标签: javascript css

这是功能:

function lastmenuborder() {
    var articleparent = document.getElementById('article').parentNode;
    var articlestyle = window.getComputedStyle(articleparent,null).getPropertyValue('borderRadius');
    alert (articlestyle);
}

我没有得到任何价值,但父节点的css是:

div#mainbody div.placeholder {
    border-radius: 3px;
}

我需要更改什么才能返回“3px”?所有人都非常感谢;我仍然是JavaScript的新手。

2 个答案:

答案 0 :(得分:10)

对于getPropertyValue(),您使用连字符而不是camelCase。

这适用于Chrome ...

.getPropertyValue('border-radius');

但Firefox似乎需要使用这种语法的特定角落......

.getPropertyValue('border-top-left-radius');

答案 1 :(得分:0)

以下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"));