cssText or individual stylename?

2019-06-16 20:58发布

When we are applying a lot of style changes using JavaScript to a single element, phpied & Writing Efficient JavaScript (slide 87) suggests:

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

Which is better when there's only a single style change?

document.getElementById('myid').style.cssText += ";color:#999;";

OR

document.getElementById('myid').style.color = "#999";

jsperf.com/csstext-vs-styles-single shows that when there's only a single style change, using individual style name is faster than cssText.

Are there any other factors also to be considered?

1条回答
女痞
2楼-- · 2019-06-16 21:41

I should use the individual stylename in your case, because you are going to change only one style. :)

查看更多
登录 后发表回答