当我们使用 JavaScript 对单个元素应用大量样式更改时,phpied & Writing Efficient JavaScript (幻灯片 87)建议:
instead of applying styles one by one using style.stylename, apply everything in one go using cssText or changing classname as it'll reduce reflows/repaints
当只有一个单一样式改变时哪个更好?
document.getElementById('myid').style.cssText += ";color:#999;";
或
document.getElementById('myid').style.color = "#999";
jsperf.com/csstext-vs-styles-single显示当只有一个样式更改时,使用单独的样式名称比 cssText 更快。
是否还有其他因素需要考虑?
最佳答案
我应该在您的案例中使用单独的样式名称,因为您将只更改一种样式。 :)
关于javascript - cssText 还是单独的样式名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254412/