语法

公鸡酸是“准备事件”的简称 鸡酮酸的断行性和链性 嵌套过滤器由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


当前回答

HTML5数据属性支持,在类固醇!

数据函数之前已经提到过。有了它,您就可以将数据与DOM元素关联起来。

最近,jQuery团队增加了对HTML5自定义data-*属性的支持。似乎这还不够;他们用类固醇强制输入数据函数,这意味着你可以直接在标记中以JSON的形式存储复杂的对象。

HTML:

<p data-xyz = '{"str": "hi there", "int": 2, "obj": { "arr": [1, 2, 3] } }' />

JavaScript:

var data = $("p").data("xyz");

data.str // "hi there"
typeof data.str // "string"

data.int + 2 // 4
typeof data.int // "number"

data.obj.arr.join(" + ") + " = 6" // "1 + 2 + 3 = 6"
typeof data.obj.arr // "object" ... Gobbles! Errrghh!

其他回答

在核心jQuery函数中,除了指定选择器参数外,还指定上下文参数。指定context参数允许jQuery从DOM中更深的分支开始,而不是从DOM根开始。给定一个足够大的DOM,指定上下文参数应该转化为性能提升。

示例:查找文档中第一个表单中radio类型的所有输入。

$("input:radio", document.forms[0]);

参考:http://docs.jquery.com/Core/jQuery # expressioncontext

无用的模式

jQuery.noConflict();

运行此函数将$变量的控制权交还给第一个实现它的库。这有助于确保jQuery不会与其他库的$ object冲突。 通过使用这个函数,你将只能通过jQuery变量访问jQuery。例如,你过去做$("div p"),你现在必须做jQuery("div p")"。

换行和可链性

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

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

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

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

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

使用.stop(true,true)当触发一个动画防止它重复动画。这对于翻转动画特别有用。

$("#someElement").hover(function(){
    $("div.desc", this).stop(true,true).fadeIn();
},function(){
    $("div.desc", this).fadeOut();
});