Created
February 18, 2014 20:35
-
-
Save sivagao/9079493 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>JavaScript 模式和反模式</title> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<script> | |
/* 题目: 重复查询 | |
* 描述: 使用jQuery的链接,可以避免重复查询 | |
*/ | |
// 反模式 | |
// 创建并附加一个元素 | |
$(document.body).append("<div class='baaron' />"); | |
// 重新查询,再绑定事件 | |
$("div.baaron").click(function () { | |
}); | |
// 模式 | |
// 使用appendTo,可以用jQuery的链接追加并绑定 | |
$("<div class='baaron' />") | |
.appendTo(document.body) | |
.click(function () { | |
}); | |
// 参考 | |
// http://paulirish.com/2009/perf/ | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment