语法

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


当前回答

在名称中增加行索引

下面是一个简单的方法来增加一个克隆的输入元素的行索引,如果你的输入元素名称包含一个像'0_row'这样的行索引:

$(this).attr('name', $(this).attr('name').replace(/^\d+/, function(n){ return ++n; }));

克隆元素的名称现在是'1_row'

其他回答

将jQuery对象保存在变量中以供重用

将jQuery对象保存为变量可以让您重用它,而不必通过DOM重新查找它。

(正如@Louis所建议的,我现在使用$表示变量保存jQuery对象。)

// Bad: searching the DOM multiple times for the same elements
$('div.foo').each...
$('div.foo').each...

// Better: saving that search for re-use
var $foos = $('div.foo');
$foos.each...
$foos.each...

举一个更复杂的例子,假设您在商店中有一个食品列表,您希望只显示符合用户标准的食品。您有一个带有复选框的表单,每个复选框包含一个标准。复选框有像有机和低脂肪这样的名称,产品有相应的类别- .organic,等等。

var $allFoods, $matchingFoods;
$allFoods = $('div.food');

现在可以继续使用jQuery对象了。每次点击一个复选框(选中或取消选中),从食物的主列表开始,并根据选中的复选框进行筛选:

// Whenever a checkbox in the form is clicked (to check or uncheck)...
$someForm.find('input:checkbox').click(function(){

  // Start out assuming all foods should be showing
  // (in case a checkbox was just unchecked)
  var $matchingFoods = $allFoods;

  // Go through all the checked boxes and keep only the foods with
  // a matching class 
  this.closest('form').find("input:checked").each(function() {  
     $matchingFoods = $matchingFoods.filter("." + $(this).attr("name")); 
  });

  // Hide any foods that don't match the criteria
  $allFoods.not($matchingFoods).hide();
});

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有.index,但使用起来很痛苦,因为你需要元素列表,并传入你想要索引的元素:

var index = e.g $('#ul>li').index( liDomObject );

下面的方法就简单多了:

如果你想知道无序列表中集合(例如列表项)中元素的索引:

$("ul > li").click(function () {
    var index = $(this).prevAll().length;
});

“准备事件”的简称

显式和详细的方式:

$(document).ready(function ()
{
    // ...
});

速记:

$(function ()
{
    // ...
});

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