语法

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


当前回答

从更基本、更高级的角度来看,您可以尝试通过编写自己的小框架来模拟jQuery的基本选择器机制(实际操作比听起来简单)。它不仅会不断地改进你的Javascript,还会帮助你理解为什么jQuery的$("#elementId")比$(". elementclass ")快很多倍,也比$("element#elementId")快很多倍(这可能在表面上与直觉相反)。

这也将迫使你学习面向对象的Javascript,这将帮助你以一种更模块化的方式组织你的代码,从而避免沉重的jQuery脚本块所带来的意大利面条式的代码性质。

其他回答

创建一个HTML元素并保留一个引用

var newDiv = $("<div />");

newDiv.attr("id", "myNewDiv").appendTo("body");

/* Now whenever I want to append the new div I created, 
   I can just reference it from the "newDiv" variable */

检查元素是否存在

if ($("#someDiv").length)
{
    // It exists...
}

编写自己的选择器

$.extend($.expr[":"], {
    over100pixels: function (e)
    {
        return $(e).height() > 100;
    }
});

$(".box:over100pixels").click(function ()
{
    alert("The element you clicked is over 100 pixels height");
});

而不是使用不同的别名为jQuery对象(当使用noConflict),我总是写我的jQuery代码包装在一个闭包。这可以在文档中完成。现成的函数:

var $ = someOtherFunction(); // from a different library

jQuery(function($) {
    if ($ instanceOf jQuery) {
        alert("$ is the jQuery object!");
    }
});

或者你可以这样做:

(function($) {
    $('...').etc()    // whatever jQuery code you want
})(jQuery);

我觉得这个最方便携带。我一直在一个同时使用Prototype和jQuery的网站上工作,这些技术避免了所有的冲突。

这个是给科比的。

考虑下面的代码片段:

// hide all elements which contains the text "abc"
$("p").each(function ()
{
    var that = $(this);

    if (that.text().indexOf("abc") > -1) that.hide();
});    

这里有一个简写……这大约是原来的两倍:

$("p.value:contains('abc')").hide();

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

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

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

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

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

jQuery文档

将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();
});