当前位置:网站首页>Creating elements of DOM series
Creating elements of DOM series
2022-07-25 02:15:00 【Old__ L】
Catalog
1、 Three ways
1、document.write()
2、element.innerHTML
3、document.createElement()
2、 difference
1、document.write() It's a content stream that writes content directly to the page , But the document flow is finished , It causes the page to be redrawn completely ;
2、innerHTML It's writing content into something DOM node , It doesn't cause the page to redraw completely ;
3、innerHTML It's more efficient to create multiple elements ,( Don't splice strings , Take the form of array splicing ), The structure is a little more complicated ;
4、createElement() Creating multiple elements is a little less efficient , But the structure is clearer .
3、 summary
Different browsers ,innerHTML More efficient than createElement high .
4、 Example
<script>
// Differences between the three methods of creating elements
// 1. document.write() Create elements If the page document stream is loaded , Calling this sentence again will cause the page to redraw
var btn = document.querySelector("button");
btn.onclick = function () {
document.write("<div>123</div>");
}
// 2. innerHTML Create elements
var inner = document.querySelector(".inner");
for (var i = 0; i <= 100; i++) {
inner.innerHTML += '<a href="#"> Baidu </a>';
}
var arr = [];
for (var i = 0; i <= 100; i++) {
arr.push('<a href="#"> Baidu </a>');
}
inner.innerHTML = arr.join("");
// 3. document.createElement() Create elements
var create = document.querySelector(".create");
for (var i = 0; i <= 100; i++) {
var a = document.createElement("a");
create.appendChild(a);
}
</script>
5、innerTHML and createElement Efficiency comparison
5.1、innerHTML String splicing ( Low efficiency )
<script>
function fn() {
var d1 = +new Date();
var str = "";
for (var i = 0; i < 1000; i++) {
document.body.innerHTML += '<div style="width:100px; height:2px; border:1px solid blue;"></div>';
}
var d2 = +new Date();
console.log(d2 - d1);
}
fn();
</script>
5.2、createElement The way ( The efficiency of general )
<script>
function fn() {
var d1 = +new Date()
for (var i = 0; i < 1000; i++) {
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "2px";
div.style.border = "1px solid red";
document.body.appendChild(div);
}
var d2 = +new Date();
console.log(d2 - d1);
}
fn();
</script>
5.3、innerHTML Array mode ( Efficient )
<script>
function fn() {
var d1 = +new Date();
var array = [];
for (var i = 0; i < 1000; i++) {
array.push('<div style="width:100px; height:2px; border:1px solid blue;"></div>');
}
document.body.innerHTML = array.join("");
var d2 = +new Date();
console.log(d2 - d1);
}
fn();
</script>
Postscript
If you feel the article is not good
//(ㄒoㄒ)//, Just leave a message in the comments , The author continues to improve ;o_O???
If you think the article is a little useful , You can praise the author ;\\*^o^*//
If you want to progress with the author , Sure Wechat scan QR code , Focus on the front-end old L;~~~///(^v^)\\\~~~
Thank you readers(^_^)∠※!!!

边栏推荐
- ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
- Unable to display spline in UE4 (unreal engine4) terrain editing tool
- "No such plugin: cloudbees folder" solution appears when Jenkins selects the recommended plug-in for the first time
- Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts
- When sharing a folder, the 'attempt to share xxxxx
- Freedom and self action Hegel
- Detailed explanation of manually writing servlet in idea
- Speed comparison between 64 bit width and 32 bit width of arm64 memory
- Research and application of scientific data management strategy for high energy synchrotron radiation source
- Cloud native platform, let edge applications play out!
猜你喜欢

Web Security Foundation - file upload

See you in Suzhou! "Cloud Intelligence Technology Forum - industry special" will be held soon

Today in history: the kotlin language was published for the first time; The father of the IMAP agreement was born; CT imaging achieves a new breakthrough

VRRP virtual redundancy protocol configuration

Green low-carbon Tianyi cloud, a new engine of digital economy!

Simulate the implementation of StrCmp function

6-10 vulnerability exploitation SMTP experimental environment construction

Talk about what's going on with C # backstage GC?

Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!

iptables :chains, target
随机推荐
DNA helped solve the outstanding case 30 years ago. The suspect strangled his girlfriend because he fell in love with his roommate. He was already the CEO of the technology company when he was arreste
A bit of knowledge - websites about scripts
Interpretation of video generation paper of fed shot video to video (neurips 2019)
Why can't reading more books improve your writing?
Four redis cluster schemes you must know and their advantages and disadvantages
Use Fiddler to capture apps
Ogg data extraction delay is large
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
POM reports an error
Yunyuanyuan (VIII) | Devops in depth Devops
BMW I3 based on clar architecture is not a simple "oil to electricity" product
[leetcode] 3. Longest substring without repeated characters - go language problem solution
Management mode of agricultural science data center based on life cycle theory
Antdb database products were selected into the global database industry map (2022) of the China Academy of communications and communications
Deep understanding of string class
Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
2022.7.20 linear table
C#/VB. Net insert watermark in word
Android memory optimized disk cache
Summary and sorting of XSS (cross site script attack) related content