jQuery 开发者应该注意的9个错误

 更新时间:2012年05月03日 23:46:10   作者:  
jQuery 开发者应该注意的9个错误,使用jquery的朋友可以参考下
jQuery is so easy to use that sometimes we just forget that it's not CSS. While using CSS, we don't have to give much thought to performance, because it's so fast that it's not worth the effort to optimize it. But when it comes to the real world, jQuery can drive developers crazy with performance issues. Sometimes you lose precious milliseconds without even noticing it. Also, it's so easy to forget about some functions and we keep using the old (and not-so-good) ones.

Let's take a look at a few of the most-common-and-easiest-to-fix mistakes while using jQuery in your projects.

1. You aren't using the latest jQuery version
Each version update means higher performance and several bug fixes. The current stable release is 1.7.2, and I'm pretty sure you know about plenty of sites developed using 1.6 and below. Ok, ok, you can't just update every old site for every jQuery update (unless your client is paying you to do so) but you should at least start using it for your new projects. So, forget about this local copy and just grab the latest release every time you start a new project.

2. You aren't using a CDN-hosted copy of jQuery
How many unique visitors you`ve got last month? I bet the number is still under 1 billion, right?
So you'd better use Google's copy of jQuery instead of yours. If your user still has the cached file of Google's website (or from many other sites that uses its CDN) his browser will just get the cached version, improving a lot your website's performance. You can use it by copying & pasting this HTML:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

3. You aren't using a fallback for CDN version
I know I said Google is awesome and stuff, but they can fail. So, every time you rely upon external sources, make sure you have a local fallback. I've seen this snippet in the HTML5 boilerplate source code and just found it amazing. You should use it too:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.2.min.js"><\/script>')</script>

4. You aren't chaining stuff
While doing common operations, you don't need to call the element every time you want to manipulate it. If you're doing several manipulations in a row, use chaining, so jQuery won't need to get the element twice.

Instead of this:

$(“#mydiv”).hide();
$(“#mydiv”).css(“padding-left”, “50px”);

Use this:

$(“#mydiv”).hide().css(“padding-left”, “50px”);

5. You aren't caching stuff
This is one of the most important performance tips. If you'll call an element at least twice, you should cache it. Caching is just saving the jQuery selector in a variable, so when you need to call it again you'll just reference the variable and jQuery won't need to search the whole DOM tree again to find your element.

/* you can use it this way because almost every jQuery function returns
the element, so $mydiv will be equal to $(“#mydiv”); also it's good to
use the $mydiv so you know it's a jQuery caching var */

var $mydiv = $(“#mydiv”).hide();
[.. lot of cool stuff going on here …]
$mydiv.show();

6. You aren't using pure js
Specially for attributes modification, we have several built in methods to get stuff done with pure javascript. You can even “convert” jQuery objects back to DOM nodes to use them with simpler methods, like this:

$mydiv[0].setAttribute('class', 'awesome'); //you can convert jQuery objects to DOM nodes using $jqObj[0]

7. You aren't checking plugins before including in your site
You know, jQuery is not the hardest thing in the world to code. But good JS (and jQuery), that is pretty hard. The bad news is that while you aren't a good programmer, you'll have to rely on trial and error to find out what is good and what isn't. A few points you must be aware of while including a plugin in your project:

File Size (the easiest to check!) – if a tooltip plugin is bigger than jQuery source, something is really wrong;
Performance – You can try it with firebug and others. They give you easy to understand charts to you'll know when something is out of place;
Cross-browsing – Test, at least on IE7, but Chrome, Firefox, Safari and Opera are good ones to try also
Mobile – Don't forget that everything is getting mobile. See if the plugin works, or at least doesn't crash your mobile browser
8. You aren't open to remove jQuery
Sometimes it's just better to remove it and use simple ol' CSS. Actually if you want, for instance, an opacity hover, or transition can be done with CSS along with good browser support. And there's no way jQuery can beat plain CSS.

9. You are using jQuery for server side tasks
I know that masking and validating are good, but don't rely just on jQuery for those. You need to recheck everything on the server side. That is especially important if you are thinking about using AJAX. Also, make sure everything will work with JS disabled.

So, it's your turn!

Do you have anything you think should be on this list? Share your thoughts!

About the Author

相关文章

  • jQuery 插件仿百度搜索框智能提示(带Value值)

    jQuery 插件仿百度搜索框智能提示(带Value值)

    因公司需要做一个仿百度搜索框,并且带Value值的, 网上找了下只有Text, 都没带Value的 所以做了个,代码给予简单的注释,结构很清晰,很容易看懂,感兴趣的朋友可以了解下
    2013-01-01
  • jQuery插件HighCharts绘制的2D堆柱状图效果示例【附demo源码下载】

    jQuery插件HighCharts绘制的2D堆柱状图效果示例【附demo源码下载】

    这篇文章主要介绍了jQuery插件HighCharts绘制的2D堆柱状图效果,结合完整实例形式分析了jQuery插件HighCharts绘制2D柱状图的实现技巧与相关注意事项,并附带demo源码供读者下载参考,需要的朋友可以参考下
    2017-03-03
  • JQuery FlexiGrid的asp.net完美解决方案 dotNetFlexGrid-.Net原生的异步表格控件

    JQuery FlexiGrid的asp.net完美解决方案 dotNetFlexGrid-.Net原生的异步表格控件

    dotNetFlexGrid是一款dotNet原生的异步表格控件,他的前身是Jquery FlexiGrid插件,我们重构了FlexiGrid的大部分Javascript代码,使其工作的更有效率,BUG更少;同时将其封装为dotNet控件,提供了简单易用的使用方式。
    2010-09-09
  • jquery 问答知识整理

    jquery 问答知识整理

    jquery 问答知识整理,学习jquery的朋友可以参考下。
    2010-02-02
  • jQuery样式操作方法整理介绍

    jQuery样式操作方法整理介绍

    这篇文章主要介绍了jQuery样式操作方法,并通过实际案例更浅显的理解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • jQuery回到顶部的代码

    jQuery回到顶部的代码

    在一些网站上,我们经常见到返回顶部的效果,本文给大家介绍基于jquery如何实现返回顶部效果,非常不错,感兴趣的朋友可以参考下
    2016-07-07
  • jquery多选项卡效果实例代码(附效果图)

    jquery多选项卡效果实例代码(附效果图)

    这个选项卡不是一个单独的选项卡,而是多个选项卡,每个选项卡里面有多个选项,而且选项的个数也不一样。废话不多说,直接上jquery代码。
    2013-03-03
  • jQuery+css3实现Ajax点击后动态删除功能的方法

    jQuery+css3实现Ajax点击后动态删除功能的方法

    这篇文章主要介绍了jQuery+css3实现Ajax点击后动态删除功能的方法,可实现点击选区后出现选区收缩、滚动消失的效果,涉及jquery结合Ajax与数学运算实时操作页面元素的相关技巧,需要的朋友可以参考下
    2015-08-08
  • Jquery 数据选择插件Pickerbox使用介绍

    Jquery 数据选择插件Pickerbox使用介绍

    目前市面上很少见或几乎没有这数据(对象)选择插件.比如,点击input , select 元素时弹出div(窗口),载入数据让用户选择数据,选择后在填充回对应的元素.
    2012-08-08
  • $.format,jquery.format 使用说明

    $.format,jquery.format 使用说明

    $.format,jquery.format 使用说明,需要的朋友可以参考下。
    2011-07-07

最新评论