语法

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


当前回答

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

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

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

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

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

jQuery文档

其他回答

异步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

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

无耻的宣传……jQuery模板插件:使用渲染函数实现复杂的逻辑

The new jQuery template plug-in is awesome. That being said, the double-curly brace template-tags are not exactly my cup of tea. In a more complex template the tags obscure the templates markup, and implementing logic past simple if/else statements is a pain. After messing around with the plug-in for a few hours, my head began to hurt from trying to distinguish the markup in my template from the millions of double curly braces. So I popped an aspirin and began work on an alternative

“准备事件”的简称

显式和详细的方式:

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

速记:

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

优化复杂选择器的性能

当使用复杂的选择器时,查询DOM的一个子集可以极大地提高性能:

var subset = $("");

$("input[value^='']", subset);

更新:

只要在站点上包含这个脚本,您就会得到一个Firebug控制台,可以在任何浏览器中进行调试。虽然功能不全,但还是很有用的!吃完之后记得把它拿掉。

<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>

看看这个链接:

来自CSS技巧

更新: 我发现了一些新的东西;它是JQuery Hotbox。

JQuery轴承箱

谷歌在谷歌Code上托管了几个JavaScript库。从那里加载它可以节省带宽,而且加载速度很快,因为它已经被缓存了。

<script src="http://www.google.com/jsapi"></script>  
<script type="text/javascript">  

    // Load jQuery  
    google.load("jquery", "1.2.6");  

    google.setOnLoadCallback(function() {  
        // Your code goes here.  
    });  

</script>

Or

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>

你也可以用它来判断图像何时完全加载。

$('#myImage').attr('src', 'image.jpg').load(function() {  
    alert('Image Loaded');  
});

firebug的"console.info",您可以使用它将消息和变量转储到屏幕上,而不必使用警告框。”控制台。Time”允许你轻松地设置一个计时器来包装一堆代码,并查看它需要多长时间。

console.time('create list');

for (i = 0; i < 1000; i++) {
    var myList = $('.myList');
    myList.append('This is list item ' + i);
}

console.timeEnd('create list');