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

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

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

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

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

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的更多相关文章

  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. Invalid Binary错误怎么解决

      Invalid Binary错误怎么解决? 本文永久地址为http://www.cnblogs.com/ChenYilong/p/3989829.html ,转载请注明出处. iOS8升级过程中, ...

  2. 再谈对协变和逆变的理解(Updated)

    去年写过一篇博客谈了下我自己对协变和逆变的理解,现在回头看发现当时还是太过“肤浅”,根本没理解.不久前还写过一篇“黑”Java泛型的博客,猛一回头又是“肤浅”,今天学习Java泛型的时候又看到了协变和 ...

  3. iOS文件存储路径规定

    Storing Your App’s Data Efficiently https://developer.apple.com/icloud/documentation/data-storage/in ...

  4. 粒子滤波particle filter和目标跟踪

    粒子滤波用于跟踪,参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/18/2404817.html http://blog.csdn.net/ ...

  5. Mac 下 Chrome 浏览器 快捷键

    ⌘-Option-I 打开“开发人员工具”. ⌘-Option-J 打开“JavaScript 控制台”. ⌘-Option-U 打开当前网页的源代码. 转自: http://www.harbin-s ...

  6. django inclusion_tag

    一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...

  7. linux下统计当前目录下文件个数

    ls | wc -l 转自:http://bbs.csdn.net/topics/60387132

  8. js ==与===区别(两个等号与三个等号)

    1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...

  9. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  10. [Android Pro] 监听内容提供者ContentProvider的数据变化

    转载自:http://blog.csdn.net/woshixuye/article/details/8281385 一.提出需求 有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者 ...