最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js中用事實(shí)證明cssText性能高的問題

 更新時(shí)間:2011年03月13日 23:49:29   作者:  
首先要感謝 EtherDream 的不同觀點(diǎn),在 巧用cssText屬性批量操作樣式 一篇中由于他的質(zhì)疑態(tài)度使我做了進(jìn)一步的測試。
復(fù)制代碼 代碼如下:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="gbk">
<title>事實(shí)證明cssText在多數(shù)瀏覽器中性能較高</title>
</head>
<body>
<input type="button" value="測試1" onclick="test1()"/> ||
<input type="button" value="測試2" onclick="test2()"/>
<div id="container"></div>
<script>
var container = document.getElementById('container');
function appendElement(){
var ary = [];
container.innerHTML = '';
for(var i=0;i<=1000;i++){
var div = document.createElement('div');
ary.push(div);
container.appendChild(div);
}
return ary;
}
function test1(){
var ary = appendElement();
var d1 = new Date;
for(var j=0;j<ary.length;j++){
ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';
}
var d2 = new Date;
console.log('耗時(shí):' + (d2-d1));
}
function test2(){
var ary = appendElement();
var d1 = new Date;
for(var j=0;j<ary.length;j++){
var sty = ary[j].style;
sty.cssText = 'width:50px;height:50px;background-color:red;';
}
var d2 = new Date;
console.log('耗時(shí):' + (d2-d1));
}
</script>
</body>
</html>

測試1,測試2都分別添加1000個(gè)div到頁面上。

測試1 使用以下三行代碼
復(fù)制代碼 代碼如下:

ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';

測試2 使用cssText一行搞定
復(fù)制代碼 代碼如下:

sty.cssText = 'width:50px;height:50px;background-color:red;';

測試1,測試2都分別添加1000個(gè)div到頁面上。

測試1 使用以下三行代碼

復(fù)制代碼 代碼如下:

ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';

測試2 使用cssText一行搞定
復(fù)制代碼 代碼如下:

sty.cssText = 'width:50px;height:50px;background-color:red;';

測試代碼:

[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]

相關(guān)文章

最新評論

喀喇沁旗| 余姚市| 四平市| 普安县| 广丰县| 仙居县| 县级市| 临江市| 常州市| 乡宁县| 湖南省| 郴州市| 志丹县| 乡宁县| 苍南县| 肇州县| 精河县| 英超| 凤阳县| 建水县| 龙口市| 安泽县| 潢川县| 西和县| 沽源县| 友谊县| 久治县| 北京市| 新兴县| 孟州市| 林西县| 若尔盖县| 西城区| 綦江县| 汨罗市| 泰来县| 石林| 江山市| 富阳市| 蓝山县| 凌云县|