forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequery.html
More file actions
42 lines (35 loc) · 840 Bytes
/
requery.html
File metadata and controls
42 lines (35 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: requery
* Description: avoid requery by using jQuery chaining
*/
// antipattern
// create and append your element
$(document.body).append("<div class='baaron' />");
// requery to bind stuff
$("div.baaron").click(function () {
});
// preferred 1
// swap to appendTo to hold your element
$("<div class='baaron' />")
.appendTo(document.body)
.click(function () {
// do stuff
});
// preferred 2
// cache the selector
var $baaron = $("<div class='baaron' />").appendTo(document.body);
$baaron.click(function () {
// do stuff
});
// References
// http://paulirish.com/2009/perf/
</script>
</body>
</html>