语法

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

$('button.someClass').live('click', someFunction);

这允许您通过ajax加载内容,或通过javascript添加内容,并自动为这些元素正确设置事件处理程序。

同样地,要停止实时事件处理:

$('button.someClass').die('click', someFunction);

与常规事件相比,这些实时事件处理程序有一些限制,但它们在大多数情况下工作得很好。

要了解更多信息,请参阅jQuery文档。

更新:live()和die()在jQuery 1.7中已弃用。参见http://api.jquery.com/on/和http://api.jquery.com/off/了解类似的替换功能。

UPDATE2: live()早在jQuery 1.7之前就已经弃用了。对于1.7之前的jQuery 1.4.2+版本,请使用delegate()和undelegate()。live()示例($('button.someClass')。live('click', someFunction);)可以像这样使用delegate()重写:$(document).delegate('button.someClass', 'click', someFunction);

其他回答

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 );

为折叠以上的元素添加一个选择器

作为jQuery的选择器插件

 $.extend($.expr[':'], {
 "aboveFold": function(a, i, m) {
   var container = m[3],
   var fold;
  if (typeof container === "undefined") {
     fold = $(window).height() + $(window).scrollTop();
  } else {
     if ($(container).length == 0 || $(container).offset().top == null) return false;
     fold = $(container).offset().top + $(container).height();
  }
  return fold >= $(a).offset().top;
 } 
});

用法:

$("p:aboveFold").css({color:"red"});

谢谢scottymac

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

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

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

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

似乎大多数有趣和重要的技巧都已经提到了,所以这一点只是一个小小的补充。

这个小技巧就是jQuery。每个(对象,回调)函数。每个人都可能使用jQuery.each(回调)函数来遍历jQuery对象本身,因为这很自然。jQuery。每个(对象,回调)实用程序函数迭代对象和数组。在很长一段时间里,除了不同的语法之外,我不知道它能做什么(我不介意写所有时髦的循环),我有点惭愧,直到最近我才意识到它的主要优点。

问题是,自从jQuery的循环体。每个(对象,回调)是一个函数,每次在循环中都获得一个新的作用域,这在循环中创建闭包时尤其方便。

换句话说,一个典型的常见错误是这样做的:

var functions = [];
var someArray = [1, 2, 3];
for (var i = 0; i < someArray.length; i++) {
    functions.push(function() { alert(someArray[i]) });
}

现在,当您调用函数数组中的函数时,您将得到三次警报,内容为undefined,这很可能不是您想要的。问题是只有一个变量i,而所有三个闭包都指向它。当循环结束时,i的最终值是3,somearray[3]是未定义的。你可以通过调用另一个函数来为你创建闭包。或者你使用jQuery实用工具,它基本上会为你做:

var functions = [];
var someArray = [1, 2, 3];
$.each(someArray, function(item) {
    functions.push(function() { alert(item) });
});

现在,当您调用这些函数时,您将得到三个内容为1、2和3的警报。

一般来说,没有什么是你自己不能做的,但拥有它是很好的。

“以”结尾的元素选择器对于ASP非常有用。NET web表单开发,因为你不需要担心前面的ctl00的愚蠢:

$("[id$='txtFirstName']");

正如注释中所指出的,如果不小心使用,这个选择器(与任何抽象层一样)可能会很慢。更倾向于将选择器作用于包含元素的范围,例如:

$(".container [id$='txtFirstName']");

减少遍历所需元素的数量。

jQuery文档