问题
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:
I should use the individual stylename in your case, because you are going to change only one style. :)
来源:https://stackoverflow.com/questions/6254412/csstext-or-individual-stylename