2010-11-22 77 views
2

未定義我有一個小的功能,將採取CSS的字符串(如background: red; width: 145px; height: 145px;) 並返回CSSStyleDeclaration但如果我有一個background屬性(background-color等)的CSSStyleDeclaration將有background-color等集在Firefox中爲undefined背景的CSSStyleDeclaration在Firefox

_parseCSS: function(css) { 
    var div = document.createElement('div'); 
    div.innerHTML = '<div style="' + css + '"></div>'; 
    return div.childNodes[0].style 
} 

有人知道我在做什麼錯嗎?

編輯:這裏是不與Firefox瀏覽器的代碼:

style: function(style) { 
    var css = this._parseCSS(style); 
    return this.each(this._elem, function(element) { 
     this.each(css, function(s) { 
     var prop = s; 
     element.style[prop] = css[s]; 
     }); 
    }); 
    }, 

參數style是CSS的字符串。

+0

適用於Firefox 3.6.9(fiddle [here](http://jsfiddle.net/U4DFA/))。什麼是您的瀏覽器版本? – 2010-11-22 21:39:57

回答

4

我認爲這是因爲連字符風格屬性在javascript意義上並不相同,background-color在JavaScript中是backgroundColor。所以,element.style.background-color應該是element.style.backgroundColor