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

时间: 2024-10-03 23:56:05

jQuery 开发者应该注意的9个错误_jquery的相关文章

解决jQuery使用JSONP时产生的错误_jquery

什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很简单的,比如使用iframe.但如果需要从另一个域加载并使用这些数据的话,就会比较麻烦.为了安全性,浏览器对这种情况有着严格的限制,需要在客户端和服务端同时做一些设置才能实现跨域请求. JSONP简介JSONP(JSON with Padding)是一种常用的跨域手段,但只支持JS脚本和JSON格式的数据

jquery.bgiframe.js在IE9下提示INVALID_CHARACTER_ERR错误_jquery

jquery.bgiframe.js在IE9下的错误 复制代码 代码如下: SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5) jquery.bgiframe.js, 行8 字符976 错误代码 复制代码 代码如下: 1 {if(!$('iframe.bgiframe',this)[0])this.insertBefore(document.createElement(html),this.firstChild);});};})(jQuery

jQuery代码性能优化的10种方法_jquery

1.总是使用#id去寻找element. 在jQuery中最快的选择器是ID选择器 ($('#someid')). 这是因为它直接映射为JavaScript的getElementById()方法. 选择单个元素 <div id="content"> <form method="post" action="/"> <h2>Traffic Light</h2> <ul id="traff

总结AngularJS开发者最常犯的十个错误_AngularJS

前言 AngularJS易于开发.较多的特征及较好的效果导致了较多的应用,伴随而来的是一些陷阱.本文列举了AngularJS的一些共同的易于出问题的地方,下面来一起看看吧. 一.MVC目录结构 AngularJS,直白地说,就是一个MVC框架.它的模型并没有像backbone.js框架那样定义的如此明确,但它的体系结构却恰如其分.当你工作于一个MVC框架时,普遍的做法是根据文件类型对其进行归类: templates/ _login.html _feed.html app/ app.js cont

.NET开发者常会忽略的几个错误

在运用Visiol studio.NET 开发Web应用程式中,开发者常常会遇到一些问题:如我开发好的程式,在开发环境下测试没问题,怎么一搬到应用环境下,就会有问题?不是程式的无法运行,就是程式的效率慢的同蜗牛在爬,这种情况在.NET的新手中尤其常见.我不知道为什么,一些介绍.NET开发的书本里引用的例子代码,也对此问题视而不见,尤其让我郁闷的是一些我喜欢的书,如:<<ADO.NET技术内幕>>,<<ASP.NET2.0高级编程(第4版)>>,这两本都是清华

10种Java开发者编写SQL语句时常见错误_MsSql

Java开发者对于面向对象编程思维与命令行编程思维的协调程度,取决于他们如下几种能力的水平: 技巧(任何人都可以编写命令行形式的代码) 教条(有的人使用"模式 - 模式"的方式,即模式无处不在,并以名字作为标识) 情绪状况(在初期,真正面向对象形式的代码比起命令式代码会更加难懂.) 但是,当Java开发人员编写SQL语句时,一切都变得不同了.SQL是一种说明式语言,与面向对象思想和命令式思想无关.在SQL语言中,查询非常容易表达.但它也不是那么容易以最佳或最正确地方式编写出来.开发人员

firefox浏览器用jquery.uploadify插件上传时报HTTP 302错误_jquery

解决了uploadify插件在chrom频繁崩溃的问题,又遇到了新问题,ff浏览器下报HTTP 302错误, ff浏览器下 uploadify 利用flash进行post上传时没有包含原来的session信息,而是重新创建了一个session,新的session无法通过登录验证,因此被重定向到了登录页面. 解决的方法无非就是将原session一起post到服务器端,然后服务器端登录验证之前将需要验证的session改为post过来的那个...(语言组织能力太差-,-). jquery.uploa

jquery validation-Jquery Validation插件在验证有错误时禁止提交

问题描述 Jquery Validation插件在验证有错误时禁止提交 Jquery Validation在输入长度不符合要求时也能提交, 怎么改成验证不通过就禁止提交? 解决方案 function check() { if(验证通过) { return true; } else { //验证不通过 return false; } }

求助啊用jquery调用一个后台方法 报500错误

问题描述 $(document).ready(function(){$("#Fnumber").bind("propertychange",function(){vara=$("#Fnumber").val();alert(a);$.ajax({type:"POST",url:"AddStorage.aspx/aa",data:"{a}",dataType:"text"