obj.style.setAttribute(name, value) 在 Chrome/Firefox 中**不支持**(CSSStyleDeclaration 無此方法),IE 早期版本支持但非標準。
**直接用括號/點語法設置 style 屬性**:
// 設置單個樣式
element.style[propertyName] = value; // 動態屬性名
element.style.propertyName = value; // 靜態屬性名(駝峰)
// 例
element.style['background-color'] = 'red'; // 支持連字符
element.style.backgroundColor = 'red'; // 推薦駝峰
element.style.cssText = 'color:red; font-size:14px;' 一次性設置多條,會覆蓋原有內聯樣式。window.getComputedStyle(element).propertyName 獲取**最終計算樣式**(含 CSS 類、繼承)。function setCellStyle(name, value, isStyleAttribute) {
try {
if (isStyleAttribute) {
o._currentCell.style[name] = value; // 直接賦值
var o_span = getSpan(o._currentCell);
if (o_span) {
o_span.style[name] = value;
}
}
} catch (ex) {
console.error(ex); // 現代調試用 console.error
}
}
棄用 style.setAttribute,改用 style[prop] = val 或 style.cssText**。這是 W3C DOM Level 2 Style 標準,全瀏覽器兼容。