jQuery要避免的陷阱 [英] jQuery pitfalls to avoid

查看:107
本文介绍了jQuery要避免的陷阱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用jQuery开始一个项目。

I am starting a project with jQuery.

你的jQuery项目中有什么陷阱/错误/误解/滥用/误用?

What pitfalls/errors/misconceptions/abuses/misuses did you have in your jQuery project?

推荐答案

不知道性能损失和过度使用选择器而不是将它们分配给局部变量。例如: -

Being unaware of the performance hit and overusing selectors instead of assigning them to local variables. For example:-

$('#button').click(function() {
    $('#label').method();
    $('#label').method2();
    $('#label').css('background-color', 'red');
});

而不是: -

$('#button').click(function() {
    var $label = $('#label');
    $label.method();
    $label.method2();
    $label.css('background-color', 'red');
});

链接更好: -

$('#button').click(function() {
    $("#label").method().method2().css("background-color", "red"); 
});

我发现这个当我意识到调用堆栈是如何工作的启发时刻。

I found this the enlightening moment when I realized how the call stacks work.

编辑:在评论中包含建议。

incorporated suggestions in comments.

这篇关于jQuery要避免的陷阱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆