【发布时间】:2014-01-27 11:12:30
【问题描述】:
我知道要替换单个样式,代码如下所示:
myDOMElement.style.height = '400px';
但是,如果我想一举完全替换整个样式对象,从而加快速度并避免重绘怎么办?例如,我想这样做:
//Get the computed style
var computedStyle = window.getComputedStyle(myDOMElement);
//Change some stuff in that CSSStyleDeclaration without rendering
computedStyle.height = '10px';
computedStyle.width = '20px';
computedStyle.whatever = 'something';
//Apply the entirety of computedStyle to the DOM Element, thereby only redrawing once
myDOMElement.style = computedStyle;
但是,当我运行此代码时,我的新样式会被忽略。我该怎么做才能解决这个问题?
【问题讨论】:
-
不幸的是,
style属性是只读的。我会想办法将computedStyle合并到 -
为什么不用css类?只需将类添加到指定的元素。
标签: javascript css dom