第一课.

ajax:$.ajax(url[, settings])

练习代码:

  1. $(document).ready(function() {
  2. $("#tour").on("click", "button", function() {
  3. $.ajax('/photos.html', {
  4. success: function(response) {
  5. $('.photos').html(response).fadeIn();
  6. }
  7. });
  8. });
  9. });
  1. <div id="tour" data-location="london">
  2. <button>Get Photos</button>
  3. <ul class="photos">
  4.  
  5. </ul>
  6. </div>

ajax 调用简写:$.get(url, success)

  1. $(document).ready(function() {
  2. $("#tour").on("click", "button", function() {
  3. $.get('/photos.html', function(response) {
  4. $('.photos').html(response).fadeIn();
  5. });
  6. });
  7. });

ajax 传递参数:直接在 url 中拼接或使用 data 对象传递,data: { "confNum": 1234 }

练习代码:获取网页元素中的 data 属性值作为 ajax 参数值传递

  1. $(document).ready(function() {
  2. $("#tour").on("click", "button", function() {
  3. $.ajax('/photos.html', {
  4. success: function(response) {
  5. $('.photos').html(response).fadeIn();
  6. },
  7. data: { "location": $("#tour").data("location")
  8. }
  9. });
  10. });
  11. });
  1. <div id="tour" data-location="london">
  2. <button>Get Photos</button>
  3. <ul class="photos">
  4.  
  5. </ul>
  6. </div>

第二课.

ajax 异常处理:error: function(request, errorType, errorMessage) { }

ajax 超时设定:timeout: 3000

ajax 调用前(beforeSend: function() {})完成后(complete: function() {})执行方法:

练习代码:

  1. $(document).ready(function() {
  2. var el = $("#tour");
  3. el.on("click", "button", function() {
  4. $.ajax('/photos.html', {
  5. data: {location: el.data('location')},
  6. success: function(response) {
  7. $('.photos').html(response).fadeIn();
  8. },
  9. error: function(request, errorType, errorMessage) {
  10. //var errmsg = $("<li>Error: " + errorType + " with message: " + errorMessage + "</li>");
  11. //$('.photos').append(errmsg);
  12. //alert('Error: ' + errorType + ' with message: ' + errorMessage);
  13. $('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
  14. },
  15. timeout: 3000,
  16. beforeSend: function() {
  17. $("#tour").addClass("is-fetching")
  18. },
  19. complete: function() {
  20. $("#tour").removeClass("is-fetching")
  21. }
  22. });
  23. });
  24. });
  1. <div id="tour" data-location="london">
  2. <button>Get Photos</button>
  3. <ul class="photos">
  4.  
  5. </ul>
  6. </div>

ajax 事件处理:处理 ajax 完成新增的标签元素的事件

练习代码:

  1. $(document).ready(function() {
  2. function showPhotos() {
  3. $(this).find('span').slideToggle();
  4. }
  5. $('.photos').on('mouseenter', 'li', showPhotos)
  6. .on('mouseleave', 'li', showPhotos);
  7.  
  8. var el = $("#tour");
  9. el.on("click", "button", function() {
  10. $.ajax('/photos.html', {
  11. data: {location: el.data('location')},
  12. success: function(response) {
  13. $('.photos').html(response).fadeIn();
  14. },
  15. error: function() {
  16. $('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
  17. },
  18. timeout: 3000,
  19. beforeSend: function() {
  20. $('#tour').addClass('is-fetching');
  21. },
  22. complete: function() {
  23. $('#tour').removeClass('is-fetching');
  24. }
  25. });
  26. });
  27. });

HTML、AJAX Result

  1. <div id="tour" data-location="london">
  2. <button>Get Photos</button>
  3. <ul class="photos">
  4.  
  5. </ul>
  6. </div>
  1. <li>
  2. <img src="/assets/photos/paris1.jpg">
  3. <span style="display: none;">Arc de Triomphe</span>
  4. </li>
  5. <li>
  6. <img src="/assets/photos/paris2.jpg">
  7. <span style="display: none;">The Eiffel Tower</span>
  8. </li>
  9. <li>
  10. <img src="/assets/photos/london.jpg">
  11. <span style="display: none;">London</span>
  12. </li>

第三课. 

JavaScript Objects:

练习代码:

重构前:

  1. $(document).ready(function() {
  2. $("#tour").on("click", "button", function() {
  3. $.ajax('/photos.html', {
  4. data: {location: $("#tour").data('location')},
  5. success: function(response) {
  6. $('.photos').html(response).fadeIn();
  7. },
  8. error: function() {
  9. $('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
  10. },
  11. timeout: 3000,
  12. beforeSend: function() {
  13. $('#tour').addClass('is-fetching');
  14. },
  15. complete: function() {
  16. $('#tour').removeClass('is-fetching');
  17. }
  18. });
  19. });
  20. });

重构为 JavaScript 对象 tour 后:

  1. var tour = {
  2. init: function() {
  3. $("#tour").on("click", "button", this.fetchPhotos);
  4. },
  5. fetchPhotos: function() {
  6. $.ajax('/photos.html', {
  7. data: {location: $("#tour").data('location')},
  8. success: function(response) {
  9. $('.photos').html(response).fadeIn();
  10. },
  11. error: function() {
  12. $('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
  13. },
  14. timeout: 3000,
  15. beforeSend: function() {
  16. $('#tour').addClass('is-fetching');
  17. },
  18. complete: function() {
  19. $('#tour').removeClass('is-fetching');
  20. }
  21. })
  22. }
  23. }
  24. $(document).ready(function() {
  25. tour.init();
  26. });

第四课.

JavaScript Functions:重点理解 this 变量的作用域


ajax 回调函数中应用调用函数变量 this 时, 需要首先在 ajax 的 context 参数中传入调用函数的 this 变量:

练习代码:

  1. function Tour(el) {
  2. var tour = this;
  3. this.el = el;
  4. this.photos = this.el.find('.photos');
  5. this.fetchPhotos = function() {
  6. $.ajax('/photos.html', {
  7. data: {location: tour.el.data('location')},
  8. context: tour,
  9. success: function(response) {
  10. this.photos.html(response).fadeIn();
  11. },
  12. error: function() {
  13. this.photos.html('<li>There was a problem fetching the latest photos. Please try again.</li>');
  14. },
  15. timeout: 3000,
  16. beforeSend: function() {
  17. this.el.addClass('is-fetching');
  18. },
  19. complete: function() {
  20. this.el.removeClass('is-fetching');
  21. }
  22. });
  23. }
  24. this.el.on('click', 'button', this.fetchPhotos);
  25. }
  26.  
  27. $(document).ready(function() {
  28. var paris = new Tour($('#paris'));
  29. var london = new Tour($('#london'));
  30. });
  1. <div id="paris" data-location="paris">
  2. <button>Get Paris Photos</button>
  3. <ul class="photos">
  4.  
  5. </ul>
  6. </div>
  7.  
  8. <div id="london" data-location="london">
  9. <button>Get London Photos</button>
  10. <ul class="photos">
  11.  
  12. </ul>
  13. </div>

第五课.

Ajax Forms

$('form').on('submit', function(event) {});  //表单提交事件

event.preventDefault(); //阻止浏览器的默认表单提交行为

type: 'POST' // POST 方式提交

data:{ "destination": $('#destination').val(), "quantity": $('#quantity').val() } //获取表单中的元素值提交数据

data: $('form').serialize() //通过表单序列化,提交整张表单中的数据

练习代码:

  1. $(document).ready(function() {
  2. $('form').on('submit', function(event) {
  3. event.preventDefault();
  4. $.ajax('/book', {
  5. type: 'POST',
  6. data: $('form').serialize(),
  7. success: function(response){
  8. $('.tour').html(response);
  9. }
  10. });
  11. });
  12. });
  1. <div class="tour" data-daily-price="357">
  2. <h2>Paris, France Tour</h2>
  3. <p>$<span id="total">2,499</span> for <span id="nights-count">7</span> Nights</p>
  4. <form action="/book" method="POST">
  5. <p>
  6. <label for="nights">Number of Nights</label>
  7. </p>
  8. <p>
  9. <input type="number" name="nights" id="nights" value="7">
  10. </p>
  11. <input type="submit" value="book">
  12. </form>
  13. </div>

第六课.

Ajax with JSON

dataType: 'json' // 通知服务器提交的数据类型为 json

contentType: 'application/json' //要求服务器返回的数据类型为 json

attr(<attribute>) //获取 html 对象的属性

attr(<attribute>, <value>) //给 html 对象的属性赋值

练习代码:

  1. $(document).ready(function() {
  2. $('form').on('submit', function(event) {
  3. event.preventDefault();
  4. $.ajax($('form').attr('action'), {
  5. type: $('form').attr('method'),
  6. data: $('form').serialize(),
  7. dataType: 'json',
  8. success: function(response) {
  9. $('.tour').html('<p></p>')
  10. .find('p')
  11. .append('Trip to ' + response.description)
  12. .append(' at $' + response.price)
  13. .append(' for ' + response.nights + ' nights')
  14. .append('. Confirmation: ' + response.confirmation);
  15. }
  16. });
  17. });
  18. });
  1. <div class="tour" data-daily-price="357">
  2. <h2>Paris, France Tour</h2>
  3. <p>$<span id="total">2,499</span> for <span id="nights-count">7</span> Nights</p>
  4. <form action="/book" method="POST">
  5. <p>
  6. <label for="nights">Number of Nights</label>
  7. </p>
  8. <p>
  9. <input type="number" name="nights" id="nights" value="7">
  10. </p>
  11. <input type="submit" value="book">
  12. </form>
  13. </div>

第七课.

Utility Methods

$.each(collection, function(<index>, <object>) {}) //iterate through the array

练习代码:

  1. $('button').on('click', function() {
  2. $.ajax('/cities/deals', {
  3. contentType: 'application/json',
  4. dataType: 'json',
  5. success: function(result) {
  6. $.each(result, function(index, dealItem) {
  7. var dealElement = $('.deal-' + index);
  8. dealElement.find('.name').html(dealItem.name);
  9. dealElement.find('.price').html(dealItem.price);
  10. });
  11. }
  12. });
  13. });

$.getJSON(url, success); //result will be an array of avaScript Objects

练习代码:

  1. $('button').on('click', function() {
  2. $.getJSON('/cities/deals', function(result) {
  3. $.each(result, function(index, dealItem) {
  4. var dealElement = $('.deal-' + index);
  5. dealElement.find('.name').html(dealItem.name);
  6. dealElement.find('.price').html(dealItem.price);
  7. });
  8. });
  9. });

$.map(collection, function(<item>, <index>){});

练习代码:

  1. $('.update-available-flights').on('click', function() {
  2. $.getJSON('/flights/late', function(result) {
  3. var flightElements = $.map(result, function(flightItem, index){
  4. var liItem = $('<li></li>');
  5. liItem.append('<p>'+flightItem.flightNumber+'</p>');
  6. liItem.append('<p>'+flightItem.time+'</p>');
  7. return liItem;
  8. });
  9. $('.flight-times').html(flightElements);
  10. });
  11. });

$.each vs $.map

.detach() //.detach() removes an element from the DOM, preserving all data and events. 

detach() 方法移除被选元素,包括所有文本和子节点。

这个方法会保留 jQuery 对象中的匹配的元素,因而可以在将来再使用这些匹配的元素。

detach() 会保留所有绑定的事件、附加的数据,这一点与 remove() 不同。

练习代码:

  1. $('.update-available-flights').on('click', function() {
  2. $.getJSON('/flights/late', function(result) {
  3. var flightElements = $.map(result, function(flightItem, index){
  4. var flightEl = $('<li>'+flightItem.flightNumber+'-'+flightItem.time+'</li>');
  5. return flightEl;
  6. });
  7. $('.flight-times').detach().html(flightElements).appendTo('.flights');
  8. });
  9. });

第八课.

Managing Events:同一个元素同一个事件挂接多个事件处理程序,按顺序执行

off(<event name>) //停止事件的监听,同时停止当前元素上指定事件的所有监听,如:$('button').off('click');

Namespacing Events:给事件监听程序命名,用于同一个元素,相同事件多个监听程序时的区分和禁用、还原等操作

trigger(<event name>):触发被选元素的指定事件类型

create a custom event:创建自定义事件后,可以通过代码触发的方式同时触发多种页面元素的监听的相同的自定义事件。

$(<dom element>).on("<event>.<namespace>", <method>)

jQuery 学习笔记(jQuery: The Return Flight)的更多相关文章

  1. jquery学习笔记---jquery插件开发

    http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html jquery插件开发:http://www.cnblogs.com/damonla ...

  2. JQuery学习笔记---jquery对象和DOM对象的关系

    1.DOM(Document  Object Model,文档对象模型).DOM树 { html (head&&body),  head(meta && title) ...

  3. jquery学习笔记----jquery相关的文档

    http://tool.oschina.net/apidocs/apidoc?api=jquery http://www.w3school.com.cn/jquery/jquery_ref_event ...

  4. JQuery学习笔记——JQuery基础

    #,JQuery避免名称冲突的方法 var jq = jQuery.noConfilct(); jq.ready( function(){     jq("p").hidden() ...

  5. jQuery学习笔记——jQuery常规选择器

    一.简单选择器在使用 jQuery 选择器时,我们首先必须使用“$()”函数来包装我们的 CSS 规则.而CSS 规则作为参数传递到 jQuery 对象内部后,再返回包含页面中对应元素的 jQuery ...

  6. jQuery学习笔记——jQuery基础核心

    代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起始的.而这个“$”就是jQuery当中最重要且独有的对象:jQuery对象,所以我们在页面元素选择或执行功能 ...

  7. jQuery 学习笔记

    jQuery 学习笔记   一.jQuery概述    宗旨: Write Less, Do More.    基础知识:        1.符号$代替document.getElementById( ...

  8. jQuery学习笔记 - 基础知识扫盲入门篇

    jQuery学习笔记 - 基础知识扫盲入门篇 2013-06-16 18:42 by 全新时代, 11 阅读, 0 评论, 收藏, 编辑 1.为什么要使用jQuery? 提供了强大的功能函数解决浏览器 ...

  9. jQuery学习笔记之Ajax用法详解

    这篇文章主要介绍了jQuery学习笔记之Ajax用法,结合实例形式较为详细的分析总结了jQuery中ajax的相关使用技巧,包括ajax请求.载入.处理.传递等,需要的朋友可以参考下 本文实例讲述了j ...

随机推荐

  1. 敏捷软件开发:原则、模式与实践——第11章 DIP:依赖倒置原则

    第11章 DIP:依赖倒置原则 DIP:依赖倒置原则: a.高层模块不应该依赖于低层模块.二者都应该依赖于抽象. b.抽象不应该依赖于细节.细节应该依赖于抽象. 11.1 层次化 下图展示了一个简单的 ...

  2. 编写高质量代码改善C#程序的157个建议——建议145:避免过长的方法和过长的类

    建议145:避免过长的方法和过长的类 如果违反“一个方法只做一件事”及类型的“单一职责原则”,往往会产生过长的方法和过长的类. 如果方法过长,意味着可以站在更高的层次上重构出若干更小的方法.以行数作为 ...

  3. Alpha冲刺(十)

    Information:   队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 过去两天完成了哪些任务 本人负责的模块(共享编辑)的前端 ...

  4. CodeForces 907F Power Tower(扩展欧拉定理)

    Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is u ...

  5. VC中CDC与HDC的区别以及二者之间的转换

    CDC是MFC的DC的一个类 HDC是DC的句柄,API中的一个类似指针的数据类型. MFC类的前缀都是C开头的 H开头的大多数是句柄 这是为了助记,是编程读\写代码的好的习惯. CDC中所有MFC的 ...

  6. SharpMap入门教程

    SharpMap是一个基于.NET Framework 4,采用C#开发的地图渲染引擎,非常易于使用.本教程针对SharpMap入门及开发,讲述如何基于SharpMap组件渲染Shapefile数据. ...

  7. docker swarm 命令

    初始化swarm manager并制定网卡地址 docker swarm init --advertise-addr 192.168.10.117 强制删除集群,如果是manager,需要加–forc ...

  8. codeforces|CF13C Sequence

    大概的题意就是每次可以对一个数加一或者减一,然后用最小的代价使得原序列变成不下降的序列. 因为是最小的代价,所以到最后的序列中的每个数字肯定在原先的序列中出现过.(大家可以想一下到底是为什么,或者简单 ...

  9. 贝塞尔曲线 WPF MVVM N阶实现 公式详解+源代码下载

    源代码下载 效果图: 本程序主要实现: N阶贝塞尔曲线(通用公式) 本程序主要使用技术 MVVM InterAction 事件绑定 动态添加Canvas的Item 第一部分公式: n=有效坐标点数量 ...

  10. php-fpm 操作命令

    以下内容转自 https://www.cnblogs.com/alibai/p/4070076.html 和 https://blog.csdn.net/wzx19840423/article/det ...