语法

公鸡酸是“准备事件”的简称 鸡酮酸的断行性和链性 嵌套过滤器由Nathan Long 缓存一个集合并通过roosteronacid在同一行上执行命令 包含选择器roosteronacid 定义元素创建时的属性 访问jQuery函数就像访问roosteronacid数组一样 noConflict函数- Oli释放$变量 通过nickf隔离noConflict模式下的$变量 无冲突模式由鸡酮酸

数据存储

数据函数-通过TenebrousX将数据绑定到元素 HTML5数据属性支持,在类固醇!由roosteronacid Filip dupanovic设计的jQuery元数据插件

优化

用鸡酮酸优化复合选择剂的性能 lupefiasco的上下文参数 Nathan Long的保存和重用搜索 创建HTML元素并保持引用,检查元素是否存在,编写自己的选择器

杂项

Check the index of an element in a collection by redsquare Live event handlers by TM Replace anonymous functions with named functions by ken Microsoft AJAX framework and jQuery bridge by Slace jQuery tutorials by egyamado Remove elements from a collection and preserve chainability by roosteronacid Declare $this at the beginning of anonymous functions by Ben FireBug lite, Hotbox plug-in, tell when an image has been loaded and Google CDN by Colour Blend Judicious use of third-party jQuery scripts by harriyott The each function by Jan Zich Form Extensions plug-in by Chris S Asynchronous each function by OneNerd The jQuery template plug-in: implementing complex logic using render-functions by roosteronacid


当前回答

优化复杂选择器的性能

当使用复杂的选择器时,查询DOM的一个子集可以极大地提高性能:

var subset = $("");

$("input[value^='']", subset);

其他回答

换行和可链性

当在集合上链接多个调用时…

$("a").hide().addClass().fadeIn().hide();

你可以用换行符增加可读性。是这样的:

$("a")
.hide()
.addClass()
.fadeIn()
.hide();

明智地使用第三方jQuery脚本,如表单字段验证或url解析。有必要了解一下它的内容,以便下次遇到JavaScript需求时了解情况。

我真的不喜欢$(document).ready(fn)快捷方式。当然,这减少了代码,但也降低了代码的可读性。当你看到$(document).ready(…)时,你知道你在看什么。$(…)被用于太多其他的方式,以至于没有立即的意义。

如果你有多个框架,你可以使用jQuery.noConflict();如你所说,但你也可以为它分配一个不同的变量,像这样:

var $j = jQuery.noConflict();

$j("#myDiv").hide();

如果你有几个框架可以归结为$x(…)风格的调用,这是非常有用的。

jQuery的data()方法很有用,但并不广为人知。它允许在不修改DOM的情况下将数据绑定到DOM元素。

Replace anonymous functions with named functions. This really supercedes the jQuery context, but it comes into play more it seems like when using jQuery, due to its reliance on callback functions. The problems I have with inline anonymous functions, are that they are harder to debug (much easier to look at a callstack with distinctly-named functions, instead 6 levels of "anonymous"), and also the fact that multiple anonymous functions within the same jQuery-chain can become unwieldy to read and/or maintain. Additionally, anonymous functions are typically not re-used; on the other hand, declaring named functions encourages me to write code that is more likely to be re-used.

一个例子;而不是:

$('div').toggle(
    function(){
        // do something
    },
    function(){
        // do something else
    }
);

我喜欢:

function onState(){
    // do something
}

function offState(){
    // do something else
}

$('div').toggle( offState, onState );