jquery最佳化02

來源:互聯網
上載者:User

標籤:blog   io   ar   os   使用   sp   java   for   strong   

ajax:

  • 使用相關函數:
    $("#file").on("click","button",function() {   $.ajax("confirmation.html",{ data: {"confNum":1234}, success: function(res) {}, error: function(req,errorType,errorMessage) {}, timeout:3000, beforeSend: function() {     $(".confirmation").addClass(‘is-loading‘); }, complete: function() {     $(".confirmation").removeClass("is-loading"); }    })});
  • 提交表單:
    $("form").on("submit",function(event) {     event.preventDefault(); //submit will reflash the page     var form = $(this);     $.ajax("/book", {     type: "POST",     data: form.serialize(), //use this;     success: function(result) {        form.remove();        $("#vacation").hide().html(result).fadeIn();       }     });   });
  • 最佳化操作:
    $.ajax(‘/test.html‘)    .done(function(res) {console.log(res,‘done1‘);})    .fail(function(res) {console.log(res,‘fail‘);})
  • 要多個ajax共同使用的時候
    $.when($.ajax("test1.html"),$.ajax("test2.html"))    .done(function(res) {console.log(res,‘done‘);})    .fail(function(res) {console.log(res,‘fail‘);});//都成功才會執行done;返回第一個ajax的res;

寫外掛程式:

  • html:
    <div class="container"><div class="col-sm-12 top"><button id="all" class="btn btn-primary col-sm-offset-10 show-price">show all</button></div><div class="jumbotron col-sm-3 vacation" data-price="110"><h4>Hawaiian Vaction</h4><button class="click btn btn-info">SHOE PRICE</button><p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p></div><div class="jumbotron col-sm-3 col-sm-offset-1 vacation" data-price="150"><h4>European Getaway</h4><button class="click btn btn-info">SHOE PRICE</button><p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p></div><div class="jumbotron col-sm-3 col-sm-offset-1 vacation" data-price="90"><h4>Visit Japan</h4><button class="click btn btn-info">SHOE PRICE</button><p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p></div></div>
  • js:
     $.fn.pricefy = function(options) {      this.each(function() {          //使用$.extend添加屬性;setting為要操作的資料集合      var settings = $.extend({      days: 3,      vacation: $(this),      price: $(this).data("price")      },options);      var show = function() {      var details = $("<p>Book" + settings.days +"days for $" + (settings.days * settings.price)+  "</p>");      settings.vacation.find(".click").hide();      settings.vacation.append(details);      };      var remove = function() {      settings.vacation.hide().off(".pricefy");      };      settings.vacation.on("click.pricefy","button",show);      settings.vacation.on("show.pricefy",show);      settings.vacation.on("click.pricefy",".remove-vacation",remove);      })      };      $(function() {      $(".vacation").pricefy();      $(".show-price").on("click",function(event) {      event.preventDefault();      $(".vacation").trigger(‘show.pricefy‘);      });      });

      

  

 

jquery最佳化02

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.