博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Best Practices and Commonly Made Mistakes When Using jQuery
阅读量:7198 次
发布时间:2019-06-29

本文共 2609 字,大约阅读时间需要 8 分钟。

 

Best Practices and Commonly Made Mistakes

 

Related Question: 

 

Remember to use $(document).ready

1.记得用 $(document).ready

 

If your code is somehow manipulating the DOM, then you need to either wrap it in a$(document).ready(function() {...}); block or move it to the end of your HTML.

 

Cache your jQuery Objects and Chain Whenever Possible

2.如果可能的话,缓存你的 jQuery 对象,并且链式使用

 

The jQuery function $() is expensive. Calling it repeatedly is extremely inefficient. Avoid doing this:

 

$('.test').addClass('hello');$('.test').css('color','orange');$('.test').prop('title','Hello world');

 

Better, cache your jQuery object in a variable:

 

var $test = $('.test');$test.addClass('hello');$test.css('color','orange');$test.prop('title','Hello world');

 

Best, chaining used to reduce repetition:

 

$('.test').addClass('hello').css('color','orange').prop('title','Hello world');

 

Variable Naming Conventions

3.变量命名约定

 

jQuery wrapped variables are usually named starting with $ to distinguish them from standard JavaScript objects.

 

var $this = $(this);

 

Know Your DOM Properties and Functions

4.知道你的DOM的属性和方法

 

While one of the goals of jQuery is to abstract away the DOM, knowing DOM properties can be extremely useful. One of the most commonly made mistakes by those who learn jQuery without learning about the DOM is to :

 

$('img').click(function(){
$(this).attr('src');// Bad!});

 

In the above code, this refers to the element from which the click event handler was fired. The code above is both slow and verbose; the code below functions identically and is much shorter, faster and readable.

 

$('img').click(function(){
this.src;// Much, much better});

 

Idiomatic Syntax for Creating Elements

5.用易读懂的方式创建元素

 

Although the following two examples seem to be functionally equivalent and syntactically correct, the first example is preferred:

 

$('

',{

text:'This is a '+ variable,"class":'blue slider', title: variable, id: variable + i}).appendTo(obj);

 

By comparison, a string concatenation approach is much less readable and far more brittle:

 

$('

This is a '+ variable +'

').appendTo(obj);

 

While the first example will be slower than the second, the benefits of greater clarity will likely outweigh the nominal speed differences in all but the most performance-sensitive applications.

 

Moreover, the idiomatic syntax is robust against the injection of special characters. For instance, in the 2nd example, a quote character in variable would prematurely close the attributes. Doing the  remains possible even if not recommended because error prone

摘自

转载于:https://www.cnblogs.com/lanfengniao/archive/2013/05/08/3067023.html

你可能感兴趣的文章
爬虫到百度贴吧,爬取自己的小说
查看>>
Windows批处理BAT脚本查询PM2.5实时空气质量指数(AQI)
查看>>
excel 函数 vlookup
查看>>
基于keepalived实现LVS高可用以及Web服务高可用
查看>>
1-2-Active Directory 域服务准备概述
查看>>
怎样测试UDP端口
查看>>
Electron实现系统鼠标指针操作
查看>>
常用的查看华为设备运行状况及排错诊断时命令(一)
查看>>
ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
查看>>
2011-09-28 18:04
查看>>
linux 断电后出现问题
查看>>
js鼠标滚轮上下滚动监听事件应用实例(跨浏览器,亲测)
查看>>
SCVMM2008R2学习(三),添加VMM库共享和库服务器
查看>>
apache显示中文乱码
查看>>
PIM-DM基本处理流程
查看>>
linux命令的参数个数限制
查看>>
JFinal aop事务 RegTxInterceptor示例
查看>>
博客添加"Fork me on GitHub"标识
查看>>
各种排序算法的稳定性分析
查看>>
OpenCascade Draw Test Harness
查看>>