语法

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


当前回答

这个是给科比的。

考虑下面的代码片段:

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

其他回答

嵌套过滤器

可以嵌套过滤器(如nickf所示)。

.filter(":not(:has(.selected))")

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

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

下面的方法就简单多了:

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

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

异步each()函数

如果你有非常复杂的文档,在迭代过程中运行jquery each()函数会锁住浏览器,并且/或Internet Explorer弹出“是否要继续运行此脚本”消息,这个解决方案将会挽救你的一天。

jQuery.forEach = function (in_array, in_pause_ms, in_callback)
{
    if (!in_array.length) return; // make sure array was sent

    var i = 0; // starting index

    bgEach(); // call the function

    function bgEach()
    {
        if (in_callback.call(in_array[i], i, in_array[i]) !== false)
        {
            i++; // move to next item

            if (i < in_array.length) setTimeout(bgEach, in_pause_ms);
        }
    }

    return in_array; // returns array
};


jQuery.fn.forEach = function (in_callback, in_optional_pause_ms)
{
    if (!in_optional_pause_ms) in_optional_pause_ms = 10; // default

    return jQuery.forEach(this, in_optional_pause_ms, in_callback); // run it
};

你可以使用它的第一种方式就像each():

$('your_selector').forEach( function() {} );

可选的第二个参数让你指定迭代之间的速度/延迟,这对动画可能很有用(下面的例子将在迭代之间等待1秒):

$('your_selector').forEach( function() {}, 1000 );

记住,因为这是异步工作的,你不能依赖于迭代在下一行代码之前完成,例如:

$('your_selector').forEach( function() {}, 500 );
// next lines of code will run before above code is complete

我为一个内部项目写了这篇文章,虽然我确信它可以改进,但它满足了我们的需要,所以希望你们中的一些人会觉得它有用。谢谢- - -

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

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

var $j = jQuery.noConflict();

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

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