Skip to content

Instantly share code, notes, and snippets.

@sivagao
Created February 18, 2014 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivagao/9079493 to your computer and use it in GitHub Desktop.
Save sivagao/9079493 to your computer and use it in GitHub Desktop.
<!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