了解jQuery技巧来提高你的代码

2020-05-24 21:18:57易采站长站整理

    </li>
</ul>
13.预加载图片

通常使用JavaScript来预加载图片是个相当不错的方法:


//定义预加载图片列表的函数(有参数)
jQuery.preloadImages = function() {
    // 遍历图片
    for ( var i = 0; i < arguments.length; i++) {
        jQuery(“<img>”).attr(“src”, arguments[i]);
    }
}
// 你可以这样使用预加载函数
$.preloadImages(“images/logo.png”, “images/logo-face.png”, “images/mission.png”);
14.将你的代码测试完好

jQuery有一个名为QUnit单元测试框架。编写测试很容易,它能让您可以放心地修改您的代码,并确保它仍然按预期工作。下面是如何工作的:

//将测试分成若干模块.
module(“Module B”);
test(“some other test”, function() {
    // 指定多少个判断语句需要加入测试中.
    expect(2);
    equals(true, false, “failing test”);
    equals(true, true, “passing test”);
});



英文原文:More jQuery and General Javascript Tips to Improve Your Code
翻译原文:http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html