File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed
Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ <!doctype html>
2+ < html lang ="en ">
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta charset ="utf-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ // antipattern
10+ // appending inside
11+ $ . each ( reallyLongArray , function ( count , item ) {
12+ var newLI = '<li>' + item + '</li>' ;
13+ $ ( '#ballers' ) . append ( newLI ) ;
14+ } ) ;
15+
16+ // documentFragment off-DOM
17+ var frag = document . createDocumentFragment ( ) ;
18+ $ . each ( reallyLongArray , function ( count , item ) {
19+ var newLI = '<li>' + item + '</li>' ;
20+ frag . appendChild ( newLI [ 0 ] ) ;
21+ } ) ;
22+ $ ( '#ballers' ) [ 0 ] . appendChild ( frag ) ;
23+
24+ // string concatenate and set innerHTML
25+ var myhtml = '' ;
26+ $ . each ( reallyLongArray , function ( count , item ) {
27+ myhtml += '<li>' + item + '</li>' ;
28+ } ) ;
29+ $ ( '#ballers' ) . html ( myhtml ) ;
30+ </ script >
31+ </ body >
32+ </ html >
Original file line number Diff line number Diff line change 1+ <!doctype html>
2+ < html lang ="en ">
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta charset ="utf-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ // antipattern
10+ // create and append your element
11+ $ ( document . body ) . append ( "<div class='baaron' />" ) ;
12+ // requery to bind stuff
13+ $ ( "div.baaron" ) . click ( function ( ) { } ) ;
14+
15+ // preferred
16+ // swap to appendTo to hold your element
17+ $ ( "<div class='baaron' />" )
18+ . appendTo ( document . body )
19+ . click ( function ( ) { } ) ;
20+ </ script >
21+ </ body >
22+ </ html >
Original file line number Diff line number Diff line change 1+ <!doctype html>
2+ < html lang ="en ">
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta charset ="utf-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ // antipattern
10+ $ ( '.buttons > *' )
11+ // preferred
12+ $ ( '.buttons' ) . children ( )
13+
14+ // antipattern
15+ $ ( '.gender :radio' )
16+ $ ( '.gender *:radio' )
17+ // preferred
18+ $ ( '.gender input:radio' )
19+ </ script >
20+ </ body >
21+ </ html >
You can’t perform that action at this time.
0 commit comments