简体   繁体   中英

Jquery single appendTo from multiple appendTo

there's a way to improve this code:

jQuery('<img />',{alt:"Logo",src:"img/logo.jpg"}).appendTo("#scrittacentro");
jQuery('<h1 />',{text:'THE LCARS COMPUTER NETWORK',class:'cLightOrange lcars'}).appendTo("#scrittacentro");
jQuery('<h2 />',{text:'Authorized Access Only',class:'cDarkBlue helvetica'}).appendTo("#scrittacentro");
jQuery('<h3 />',{text:'Please Report Malfunctions To Engineering Staff On Duty',class:'cDarkBlue helvetica'}).appendTo("#scrittacentro");

On something like this:

jQuery(
    '<img />',{alt:"Logo",src:"img/logo.jpg"},
    '<h1 />',{text:'THE LCARS COMPUTER NETWORK',class:'cLightOrange lcars'},
    '<h2 />',{text:'Authorized Access Only',class:'cDarkBlue helvetica'},
    '<h3 />',{text:'Please Report Malfunctions To Engineering Staff On Duty',class:'cDarkBlue helvetica'
}).appendTo("#scrittacentro");

Thank you in advance.

In Multiple appendTo you are calling jquery constructor many times which makes the code slower and decreases the performance.

In single appenTo you invoking jquery constructor one time which increases the performance and doing the same work in faster manner

Check The comparison of performance http://jsperf.com/appendto-vs-multiple-appendto

Single appendTo is better

Your code can be minified

js

jQuery('<img alt="Logo" src="img/logo.jpg"><h1 class="cLightOrange lcars">THE LCARS COMPUTER NETWORK</h1><h2 class="cDarkBlue helvetica">Authorized Access Only</h2><h3 class="cDarkBlue helvetica">Please Report Malfunctions To Engineering Staff On Duty</h3').appendTo("body");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM