缓存变量:DOM遍历是昂贵的,所以尽量将会重用的元素缓存。

  1. $element = $('#element');
  2. h = $element.height(); //缓存
  3. $element.css('height',h-20);

如果你打算对DOM元素做大量操作(连续设置多个属性或css样式),建议首先分离元素然后在添加。

使用var与匈牙利命名法,且避免全局变量 ;  

优化选择符;避免多个ID选择符

ajax:

  • 使用相关函数:

    1. $("#file").on("click","button",function() {
    2. $.ajax("confirmation.html",{
    3. data: {"confNum":1234},
    4. success: function(res) {},
    5. error: function(req,errorType,errorMessage) {},
    6. timeout:3000,
    7. beforeSend: function() {
    8. $(".confirmation").addClass('is-loading');
    9. },
    10. complete: function() {
    11. $(".confirmation").removeClass("is-loading");
    12. }
    13. })
    14. });
  • 提交表单:
    1. $("form").on("submit",function(event) {
    2. event.preventDefault(); //submit will reflash the page
    3. var form = $(this);
    4. $.ajax("/book", {
    5. type: "POST",
    6. data: form.serialize(), //use this;
    7. success: function(result) {
    8. form.remove();
    9. $("#vacation").hide().html(result).fadeIn();
    10. }
    11. });
    12. });
  • 优化操作:
    1. $.ajax('/test.html')
    2. .done(function(res) {console.log(res,'done1');})
    3. .fail(function(res) {console.log(res,'fail');})
  • 要多个ajax共同使用的时候
    1. $.when($.ajax("test1.html"),$.ajax("test2.html"))
    2. .done(function(res) {console.log(res,'done');})
    3. .fail(function(res) {console.log(res,'fail');});
    4.  
    5. //都成功才会执行done;返回第一个ajax的res;

写插件:

  • html:

    1. <div class="container">
    2. <div class="col-sm-12 top">
    3. <button id="all" class="btn btn-primary col-sm-offset-10 show-price">show all</button>
    4. </div>
    5. <div class="jumbotron col-sm-3 vacation" data-price="110">
    6. <h4>Hawaiian Vaction</h4>
    7. <button class="click btn btn-info">SHOE PRICE</button>
    8. <p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p>
    9. </div>
    10.  
    11. <div class="jumbotron col-sm-3 col-sm-offset-1 vacation" data-price="150">
    12. <h4>European Getaway</h4>
    13. <button class="click btn btn-info">SHOE PRICE</button>
    14. <p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p>
    15. </div>
    16.  
    17. <div class="jumbotron col-sm-3 col-sm-offset-1 vacation" data-price="90">
    18. <h4>Visit Japan</h4>
    19. <button class="click btn btn-info">SHOE PRICE</button>
    20. <p class="no-hide"><a href="#" class="remove-vacation">no thanks!</a></p>
    21. </div>
    22. </div>
  • js:
    1. $.fn.pricefy = function(options) {
    2. this.each(function() {
    3. //使用$.extend添加属性;setting为要操作的数据集合
    4. var settings = $.extend({
    5. days: 3,
    6. vacation: $(this),
    7. price: $(this).data("price")
    8. },options);
    9. var show = function() {
    10. var details = $("<p>Book" + settings.days +"days for $" + (settings.days * settings.price)+ "</p>");
    11. settings.vacation.find(".click").hide();
    12. settings.vacation.append(details);
    13. };
    14. var remove = function() {
    15. settings.vacation.hide().off(".pricefy");
    16. };
    17. settings.vacation.on("click.pricefy","button",show);
    18. settings.vacation.on("show.pricefy",show);
    19. settings.vacation.on("click.pricefy",".remove-vacation",remove);
    20. })
    21. };
    22.  
    23. $(function() {
    24. $(".vacation").pricefy();
    25. $(".show-price").on("click",function(event) {
    26. event.preventDefault();
    27. $(".vacation").trigger('show.pricefy');
    28. });
    29. });

      

      

  

jquery优化02的更多相关文章

  1. 新手必看的jQuery优化笔记十则

    jQuery优化 1.简介 jQuery正在成为Web开发人员首选的JavaScript库,作为Web开发者,除了要了解语言和框架的应用技巧外,如何提升语言本身的性能也是开发人员应该思考的问题.文章就 ...

  2. jquery优化引发的思考

    无意间看到jquery优化的一个细节让我觉得不可思议记录一下.仅仅只是换个地方代码就能提高数倍的效率,带给我的不是个仅是个小技巧,而是一总编程思想,技术大牛往往是在细节上体现. 通过缓存最小化选择操作 ...

  3. jquery优化28个建议

    我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来.我也做了一个jQuery性能优化的简明样式表,你可 ...

  4. jquery优化轮播图2

    继续优化 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  5. jquery优化

    选择器优化执行的速度 选择器 优先:id>元素>类 使用对象缓存:即使用变量来保存对象名,var $myDiv = $("#myDiv"):$myDiv.show(); ...

  6. jQuery优化性能的十种方法

    1,总是从ID选择器开始继承 例如: <div id="content"> <form method="post" action=" ...

  7. ☀【jQuery 优化】jQuery基础教程(第3版)

    jQuery代码优化:选择符篇 √ http://www.ituring.com.cn/article/377 jQuery代码优化:遍历篇 √ http://www.ituring.com.cn/a ...

  8. jQuery 学习02——效果:隐藏/显示、淡入淡出、滑动、动画、停止动画、Callback、链

    jQuery 效果- 隐藏hide()和显示show() 语法: $(selector).hide(speed,callback);$(selector).show(speed,callback); ...

  9. jQuery总结02

    1 如何搭建一个 jQuery 环境? 2 jQuery 对象与 DOM 对象一样吗?区别是什么? 3 jQuery选择器类型有哪些?

随机推荐

  1. C# JavascriptSerializer与匿名对象打造Json的完美工具

    一:背景 在web项目中经常需要生成json数据,返回给前端ajax. 无论是ashx,还是WebMethod,可以人工的用字符串去拼接,最终得到json数据. 有没有更好的方法呢?我个人推荐使用Ja ...

  2. (源码)自己写的ScrollView里套漂亮的圆角listview(算是漂亮吧。。。)

    找了相关的资料终于写完了: http://blog.csdn.net/jamin0107/article/details/6973845 和 http://emmet1988.iteye.com/bl ...

  3. iOS应用IAP设置总结

    iOS应用调置 wjforstudy分享了IAP的一些基本知识.在论坛的地址是:http://www.cocoachina.com/bbs/read.php?tid=92060  1.在开始IAP开发 ...

  4. iphone/ipad图标尺寸

    http://www.yixieshi.com/ucd/13759.html APP界面设计规范指导APP设计过程中的设计标准,根据统一的设计标准,使得整个APP在视觉上统一.提高用户对APP的产品认 ...

  5. [POJ1157]LITTLE SHOP OF FLOWERS

    [POJ1157]LITTLE SHOP OF FLOWERS 试题描述 You want to arrange the window of your flower shop in a most pl ...

  6. SQL注入--宽字节注入

    PHP测试代码: <?php // 面向对象写法 $id=addslashes($_GET[‘id’]); //获取id并转义预定义字符 // /$id=$_GET[‘id’]; $mysqli ...

  7. Android使用OkHttp实现带进度的上传下载

    先贴上MainActivity.java package cn.edu.zafu.sample; import android.os.Bundle; import android.support.v7 ...

  8. HDU2191多重背包例题

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d ...

  9. HDOJ 1690

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. [ruby on rails] 跟我学之(7)创建数据

    通过form来创建数据,本章节将会涉及内容:创建form,用户重导向,渲染views 和 flash消息. 1. views初步 编辑 app/views/posts/index.html.erb这个 ...