本文翻译自:jQuery Tips and Tricks
Syntax 句法
- Shorthand for the ready-event by roosteronacid 由roosteronacid 准备的事件的简写
- Line breaks and chainability by roosteronacid 通过roosteronacid的断裂和可连接性
- Nesting filters by Nathan Long 由Nathan Long 嵌套过滤器
- Cache a collection and execute commands on the same line by roosteronacid 通过roosteronacid 缓存集合并在同一行执行命令
- Contains selector by roosteronacid 由roosteronacid 包含选择器
- Defining properties at element creation by roosteronacid 通过roosteronacid 定义元素创建时的属性
- Access jQuery functions as you would an array by roosteronacid 通过roosteronacid 访问jQuery函数和数组一样
- The noConflict function - Freeing up the $ variable by Oli noConflict函数 -由Oli 释放$变量
- Isolate the $ variable in noConflict mode by nickf 通过nickf 在noConflict模式下隔离$变量
- No-conflict mode by roosteronacid roosteronacid的无冲突模式
Data Storage 数据存储
- The data function - bind data to elements by TenebrousX 数据功能 -通过TenebrousX 将数据绑定到元素
- HTML5 data attributes support, on steroids! 类固醇支持HTML5数据属性! by roosteronacid 通过roosteronacid
- The jQuery metadata plug-in by Filip Dupanović FilipDupanović 的jQuery元数据插件
Optimization 优化
- Optimize performance of complex selectors by roosteronacid 通过roosteronacid 优化复杂选择器的性能
- The context parameter by lupefiasco lupefiasco 的上下文参数
- Save and reuse searches by Nathan Long 保存并重复使用 Nathan Long的搜索
- Creating an HTML Element and keeping a reference, Checking if an element exists, Writing your own selectors by Andreas Grech 创建HTML元素并保留引用,检查元素是否存在,由Andreas Grech 编写自己的选择器
Miscellaneous 杂
- Check the index of an element in a collection by redsquare 通过redsquare 检查集合中元素的索引
- Live event handlers by TM TM的现场活动处理程序
- Replace anonymous functions with named functions by ken 用 ken 替换匿名函数和命名函数
- Microsoft AJAX framework and jQuery bridge by Slace Slace的Microsoft AJAX框架和jQuery桥接器
- jQuery tutorials by egyamado egyamado的jQuery教程
- Remove elements from a collection and preserve chainability by roosteronacid 从集合中移除元素并通过roosteronacid 保持可链接性
- Declare $this at the beginning of anonymous functions by Ben 在 Ben 的匿名函数开头声明$ this
- FireBug lite, Hotbox plug-in, tell when an image has been loaded and Google CDN by Colour Blend FireBug lite,Hotbox插件,告知何时加载图像和 Color Blend的Google CDN
- Judicious use of third-party jQuery scripts by harriyott harriyott 明智地使用第三方jQuery脚本
- The each function by Jan Zich Jan Zich 的每个功能
- Form Extensions plug-in by Chris S Chris S的Form Extensions插件
- Asynchronous each function by OneNerd 由OneNerd 异步的每个函数
- The jQuery template plug-in: implementing complex logic using render-functions by roosteronacid jQuery模板插件:使用 roosteronacid的render函数实现复杂的逻辑
#1楼
参考:https://stackoom.com/question/lVe/jQuery提示和技巧
#2楼
Optimize performance of complex selectors 优化复杂选择器的性能
Query a subset of the DOM when using complex selectors drastically improves performance: 使用复杂选择器时,查询DOM的子集可以显着提高性能:
var subset = $("");
$("input[value^='']", subset);
#3楼
It seems that most of the interesting and important tips have been already mentioned, so this one is just a little addition. 似乎已经提到了大多数有趣且重要的提示,所以这只是一点补充。
The little tip is the jQuery.each(object, callback) function. 小技巧是jQuery.each(对象,回调)函数。 Everybody is probably using the jQuery.each(callback) function to iterate over the jQuery object itself because it is natural. 每个人都可能使用jQuery.each(回调)函数迭代jQuery对象本身,因为它很自然。 The jQuery.each(object, callback) utility function iterates over objects and arrays. jQuery.each(对象,回调)实用程序函数迭代对象和数组。 For a long time, I somehow did not see what it could be for apart from a different syntax (I don't mind writing all fashioned loops), and I'm a bit ashamed that I realized its main strength only recently. 很长一段时间,我不知道除了不同的语法之外没有看到它是什么(我不介意编写所有时尚的循环),而且我有点惭愧,因为我最近才意识到它的主要优势。
The thing is that since the body of the loop in jQuery.each(object, callback) is a function , you get a new scope every time in the loop which is especially convenient when you create closures in the loop. 问题在于,由于jQuery.each(对象,回调)中的循环体是一个函数 ,因此每次在循环中都会得到一个新的作用域 ,这在循环中创建闭包时特别方便。
In other words, a typical common mistake is to do something like: 换句话说,一个典型的常见错误是执行以下操作:
var functions = [];
var someArray = [1, 2, 3];
for (var i = 0; i < someArray.length; i++) {
functions.push(function() { alert(someArray[i]) });
}
Now, when you invoke the functions in the functions
array, you will get three times alert with the content undefined
which is most likely not what you wanted. 现在,当您调用functions
数组中的functions
,您将获得三次警报,内容undefined
,这很可能不是您想要的。 The problem is that there is just one variable i
, and all three closures refer to it. 问题是只有一个变量i
,所有三个闭包都引用它。 When the loop finishes, the final value of i
is 3, and someArrary[3]
is undefined
. 当循环结束时, i
的最终值为3, someArrary[3]
undefined
。 You could work around it by calling another function which would create the closure for you. 您可以通过调用另一个为您创建闭包的函数来解决它。 Or you use the jQuery utility which it will basically do it for you: 或者您使用jQuery实用程序,它基本上会为您执行此操作:
var functions = [];
var someArray = [1, 2, 3];
$.each(someArray, function(item) {
functions.push(function() { alert(item) });
});
Now, when you invoke the functions you get three alerts with the content 1, 2 and 3 as expected. 现在,当您调用这些函数时,您将获得三个警报,其中包含内容1,2和3。
In general, it is nothing you could not do yourself, but it's nice to have. 一般来说,这是你自己无法做到的事情,但它很高兴。
#4楼
Update: 更新:
Just include this script on the site and you'll get a Firebug console that pops up for debugging in any browser. 只需在网站上包含此脚本,您就可以在任何浏览器中弹出一个Firebug控制台进行调试。 Not quite as full featured but it's still pretty helpful! 不完整的功能,但它仍然非常有用! Remember to remove it when you are done. 记得在完成后将其删除。
<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
Check out this link: 看看这个链接:
Update: I found something new; 更新:我找到了新的东西; its the the JQuery Hotbox. 它是JQuery Hotbox。
Google hosts several JavaScript libraries on Google Code. Google在Google Code上托管了多个JavaScript库。 Loading it from there saves bandwidth and it loads quick cos it has already been cached. 从那里加载它可以节省带宽,并加载已经缓存的快速cos。
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load jQuery
google.load("jquery", "1.2.6");
google.setOnLoadCallback(function() {
// Your code goes here.
});
</script>
Or 要么
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
You can also use this to tell when an image is fully loaded. 您还可以使用它来判断图像何时完全加载。
$('#myImage').attr('src', 'image.jpg').load(function() {
alert('Image Loaded');
});
The "console.info" of firebug, which you can use to dump messages and variables to the screen without having to use alert boxes. firebug的“console.info”,您可以使用它将消息和变量转储到屏幕而不必使用警告框。 "console.time" allows you to easily set up a timer to wrap a bunch of code and see how long it takes. “console.time”允许您轻松设置一个计时器来包装一堆代码并查看它需要多长时间。
console.time('create list');
for (i = 0; i < 1000; i++) {
var myList = $('.myList');
myList.append('This is list item ' + i);
}
console.timeEnd('create list');
#5楼
I'm really not a fan of the $(document).ready(fn)
shortcut. 我真的不喜欢$(document).ready(fn)
快捷方式。 Sure it cuts down on the code but it also cuts way down on the readability of the code. 当然它减少了代码,但它也减少了代码的可读性。 When you see $(document).ready(...)
, you know what you're looking at. 当你看到$(document).ready(...)
,你知道你在看什么。 $(...)
is used in far too many other ways to immediately make sense. $(...)
用于太多其他方式立即有意义。
If you have multiple frameworks you can use jQuery.noConflict();
如果你有多个框架,你可以使用jQuery.noConflict();
as you say, but you can also assign a different variable for it like this: 如你所说,但你也可以为它分配一个不同的变量:
var $j = jQuery.noConflict();
$j("#myDiv").hide();
Very useful if you have several frameworks that can be boiled down to $x(...)
-style calls. 如果您有几个可以归结为$x(...)
式调用的框架,那将非常有用。
#6楼
Judicious use of third-party jQuery scripts, such as form field validation or url parsing. 明智地使用第三方jQuery脚本,例如表单字段验证或URL解析。 It's worth seeing what's about so you'll know when you next encounter a JavaScript requirement. 值得一看的是什么,以便您在下次遇到JavaScript要求时知道。