- Ajax
- jQuery ready事件
- 事件处理
- DOM操作
Ajax
大部分项目这么写:
- function getName(personid) {
- var dynamicData = {};
- dynamicData["id"] = personID;
- $.ajax({
- url: "getName.php",
- type: "get",
- data: dynamicData,
- success: function(data) {
- // Updates the UI based the ajax result
- $(".person-name").text(data.name);
- }
- });
- }
- getName("2342342");
function getName(personid) { var dynamicData = {}; dynamicData["id"] = personID; $.ajax({ url: "getName.php", type: "get", data: dynamicData, success: function(data) { // Updates the UI based the ajax result $(".person-name").text(data.name); } }); } getName("2342342");
最佳实践:
- function getName(personid) {
- var dynamicData = {};
- dynamicData["id"] = personid;
- return $.ajax({
- url: "getName.php",
- type: "get",
- data: dynamicData
- });
- }
- getName("2342342").done(function(data) {
- // Updates the UI based the ajax result
- $(".person-name").text(data.name);
- });
function getName(personid) { var dynamicData = {}; dynamicData["id"] = personid; return $.ajax({ url: "getName.php", type: "get", data: dynamicData }); } getName("2342342").done(function(data) { // Updates the UI based the ajax result $(".person-name").text(data.name); });
非常灵活
jQuery ready事件
大部分项目用这段代码做闭包
- $("document").ready(function() {
- // The DOM is ready!
- // The rest of the code goes here
- });
$("document").ready(function() { // The DOM is ready! // The rest of the code goes here });
或者用简写
- $(function() {
- // The DOM is ready!
- // The rest of the code goes here
- });
$(function() { // The DOM is ready! // The rest of the code goes here });
- 如果你清楚代码的执行环境
- 如果你不关注页面加载性能
- 如果你不关注最佳实践
。。。这么写就没问题
更好的写法是
- // IIFE - Immediately Invoked Function Expression
- (function($, window, document) {
- // The $ is now locally scoped
- // Listen for the jQuery ready event on the document
- $(function() {
- // The DOM is ready!
- });
- // The rest of the code goes here!
- }(window.jQuery, window, document));
- // The global jQuery object is passed as a parameter
// IIFE - Immediately Invoked Function Expression (function($, window, document) { // The $ is now locally scoped // Listen for the jQuery ready event on the document $(function() { // The DOM is ready! }); // The rest of the code goes here! }(window.jQuery, window, document)); // The global jQuery object is passed as a parameter
更进一步,最佳写法:
- // IIFE - Immediately Invoked Function Expression
- (function(yourcode) {
- // The global jQuery object is passed as a parameter
- yourcode(window.jQuery, window, document);
- }(function($, window, document) {
- // The $ is now locally scoped
- // Listen for the jQuery ready event on the document
- $(function() {
- // The DOM is ready!
- }));
// IIFE - Immediately Invoked Function Expression (function(yourcode) { // The global jQuery object is passed as a parameter yourcode(window.jQuery, window, document); }(function($, window, document) { // The $ is now locally scoped // Listen for the jQuery ready event on the document $(function() { // The DOM is ready! }));
事件处理
大部分项目这么写:
- $("#longlist li").on("mouseenter", function() {
- $(this).text("Click me!");
- });
- $("#longlist li").on("click", function() {
- $(this).text("Why did you click me?!");
- });
$("#longlist li").on("mouseenter", function() { $(this).text("Click me!"); }); $("#longlist li").on("click", function() { $(this).text("Why did you click me?!"); });
更好的写法:
- var listItems = $("#longlist li");
- listItems.on({
- "mouseenter": function() {
- $(this).text("Click me!");
- },
- "click": function() {
- $(this).text("Why did you click me?!");
- }
- });
var listItems = $("#longlist li"); listItems.on({ "mouseenter": function() { $(this).text("Click me!"); }, "click": function() { $(this).text("Why did you click me?!"); } });
DRY ( Don't repeat yourself. )
最佳实践:
- var list = $("#longlist");
- list.on("mouseenter", "li", function(){
- $(this).text("Click me!");
- });
- list.on("click", "li", function() {
- $(this).text("Why did you click me?!");
- });
var list = $("#longlist"); list.on("mouseenter", "li", function(){ $(this).text("Click me!"); }); list.on("click", "li", function() { $(this).text("Why did you click me?!"); });
使用事件代理(Event Delegation)
DOM操作
大部分项目这么写:
- $('.class1').click(function() {
- some_function();
- });
- $('.class2').click(function() {
- some_function();
- });
$('.class1').click(function() { some_function(); }); $('.class2').click(function() { some_function(); });
- 如果你喜欢重复的编码
- 如果你不关心代码性能
- 如果你不关注最佳实践
更好的实现方法:
- $('.class1').$('.class2').click(function() {
- some_function();
- });
$('.class1').$('.class2').click(function() { some_function(); });
大部分项目这么写:
- / Set's an element's title attribute using it's current text
- $(".container input#elem").attr("title", $(".container input#elem").text());
- // Set's an element's text color to red
- $(".container input#elem").css("color", "red");
- // Makes the element fade out
- $(".container input#elem").fadeOut();
/ Set's an element's title attribute using it's current text $(".container input#elem").attr("title", $(".container input#elem").text()); // Set's an element's text color to red $(".container input#elem").css("color", "red"); // Makes the element fade out $(".container input#elem").fadeOut();
- 如果你喜欢重复的编码
- 如果你不关心代码性能
- 如果你不关注最佳实践
。。。这么写没问题
更好的实现方法:
- // Set's an element's title attribute using it's current text
- $("#elem").attr("title", $("#elem").text());
- // Set's an element's text color to red
- $("#elem").css("color", "red");
- // Makes the element fade out
- $("#elem").fadeOut();
// Set's an element's title attribute using it's current text $("#elem").attr("title", $("#elem").text()); // Set's an element's text color to red $("#elem").css("color", "red"); // Makes the element fade out $("#elem").fadeOut();
简化你的选择器
最佳实践:
- // Stores the live DOM element inside of a variable
- var elem = $("#elem");
- // Set's an element's title attribute using it's current text
- elem.attr("title", elem.text());
- // Set's an element's text color to red
- elem.css("color", "red");
- // Makes the element fade out
- elem.fadeOut();
// Stores the live DOM element inside of a variable var elem = $("#elem"); // Set's an element's title attribute using it's current text elem.attr("title", elem.text()); // Set's an element's text color to red elem.css("color", "red"); // Makes the element fade out elem.fadeOut();
使用变量缓存你的选择器
或者是用更简单的写法:
- // Stores the live DOM element inside of a variable
- var elem = $("#elem");
- // Chaining
- elem.attr("title", elem.text()).css("color", "red").fadeOut();
// Stores the live DOM element inside of a variable var elem = $("#elem"); // Chaining elem.attr("title", elem.text()).css("color", "red").fadeOut();
使用链式调用
另外一个操作DOM的示例
大部分项目这么写:
- // Dynamically building an unordered list from an array
- var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"],
- list = $("ul.people");
- $.each(localArr, function(index, value) {
- list.append("<li id=" + index + ">" + value + "</li>");
- });
// Dynamically building an unordered list from an array var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"], list = $("ul.people"); $.each(localArr, function(index, value) { list.append("<li id=" + index + ">" + value + "</li>"); });
最佳实践:只append一次
- // Dynamically building an unordered list from an array
- var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"],
- list = $("ul.people"),
- dynamicItems = "";
- $.each(localArr, function(index, value) {
- dynamicItems += "<li id=" + index + ">" + value + "</li>";
- });
- list.append(dynamicItems);
// Dynamically building an unordered list from an array var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"], list = $("ul.people"), dynamicItems = ""; $.each(localArr, function(index, value) { dynamicItems += "<li id=" + index + ">" + value + "</li>"; }); list.append(dynamicItems);
参考资料:
http://gregfranko.com/jquery-best-practices/
http://stackoverflow.com/questions/18307078/jquery-best-practises-in-case-of-document-ready
http://gregfranko.com/jquery-best-practices/#/29
相关推荐
联想工程师专用小工具 系统优化加速工具V1.37.1联想工程师专用小工具 系统优化加速工具V1.37.1联想工程师专用小工具 系统优化加速工具V1.37.1联想工程师专用小工具 系统优化加速工具V1.37.1联想工程师专用小工具 ...
Java工程师不得不看的几千份代码Java工程师不得不看的几千份代码Java工程师不得不看的几千份代码
拓扑优化88行,是在经典的99行基础上改进的,精简的,这代码对从事结构设计,优化的研究这者,工程师具有很好的价值。
联想工程师专用小工具 win8_10无线连接受限优化工具V2.45.1联想工程师专用小工具 win8_10无线连接受限优化工具V2.45.1联想工程师专用小工具 win8_10无线连接受限优化工具V2.45.1联想工程师专用小工具 win8_10无线...
高级网络优化工程师面试必备的能力,若要面试高级网络优化工程师,必须掌握的技能
联想工程师专用小工具 磁盘优化V3.03.1联想工程师专用小工具 磁盘优化V3.03.1联想工程师专用小工具 磁盘优化V3.03.1联想工程师专用小工具 磁盘优化V3.03.1联想工程师专用小工具 磁盘优化V3.03.1联想工程师专用小工具...
联想工程师专用小工具 IE优化工具V1.55.1联想工程师专用小工具 IE优化工具V1.55.1联想工程师专用小工具 IE优化工具V1.55.1联想工程师专用小工具 IE优化工具V1.55.1联想工程师专用小工具 IE优化工具V1.55.1联想工程师...
本人通过了网络工程师的考试,通过对考试中的大题的理解,认真整理的,希望,可以满意,
高级无线网络优化工程师面试经验总结超级经典版-最佳适用于G网高级无线网优优化工程师面试,中兴、华为、爱立信、大唐、诺西等厂商及移动、联通、电信有关无线网优的经典面试必备良药,其他也可以参考。
达内C/C++软件工程师全部代码,欢迎下载!
软件工程师必备的开发文档,里面讲述的如何编写高质量的代码,高质量的代码可以使软件的执行效率提高、重用性增强、维护难度降低。
用以帮助pb开发者美化代码 一步注释,提高pb开发工程师编写代码的效率
2022年无线网络优化工程师考试试题.doc
教你如何优化代码、提高代码的执行效率,压缩Flash空间,适合AVR系列单片机初学工程师
程序员绩效考核标准,软件开发工程师考核项,软件工程师岗位考核指标,程序员代码评审标准,软件工程师代码评审标准,程序员绩效考核。软件开发工程师考核项,软件工程师岗位考核指标,程序员绩效考核标准, 程序员...
北大青鸟.软件工程师.4.0.教学PPT和源代码
代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 适用对象:工科生、数学专业、算法等方向学习者。 作者介绍:某大厂资深算法工程师,从事Matlab、Python、C/C++、Java算法仿真工作10年;擅长...
联想工程师专用小工具 常见系统优化维护工具V1.98.1联想工程师专用小工具 常见系统优化维护工具V1.98.1联想工程师专用小工具 常见系统优化维护工具V1.98.1联想工程师专用小工具 常见系统优化维护工具V1.98.1联想...
现在大量网站为了追求用户体验,使用了大量使用CSS和JS文件。...如果英文阅读不是很流畅,请查看翻译版本: 《Yahoo工程师的前端优化建议-- 中文翻译版》 《Yahoo工程师的前端优化建议-- 中文翻译版》
Python全栈数据工程师养成攻略-全部代码