js中用事实证明cssText性能高的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了js中用事实证明cssText性能高的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1633字,纯文字阅读大概需要3分钟。
内容图文
![js中用事实证明cssText性能高的问题](/upload/InfoBanner/zyjiaocheng/370/b7d13c4212d74f73ba77da02b57eae55.jpg)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gbk">
<title>事实证明cssText在多数浏览器中性能较高</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('耗时:' + (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('耗时:' + (d2-d1));
}
</script>
</body>
</html>
测试1,测试2都分别添加1000个div到页面上。
测试1 使用以下三行代码
代码如下:
ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';
测试2 使用cssText一行搞定
代码如下:
sty.cssText = 'width:50px;height:50px;background-color:red;';
测试1,测试2都分别添加1000个div到页面上。
测试1 使用以下三行代码
代码如下:
ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';
测试2 使用cssText一行搞定
代码如下:
sty.cssText = 'width:50px;height:50px;background-color:red;';
测试代码:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
内容总结
以上是互联网集市为您收集整理的js中用事实证明cssText性能高的问题全部内容,希望文章能够帮你解决js中用事实证明cssText性能高的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。