【问题标题】:Making jQuery statements shorter使 jQuery 语句更短
【发布时间】:2009-07-28 13:26:01
【问题描述】:

我有以下 jQuery 语句,它可以正常工作,但我想知道它是否可以缩短,是否更简洁会提高性能?

奇怪的起始标签的原因是它是一个wordpress主题。

提前干杯!

jQuery(document).ready(function($) {

$(".sidebar ol#navigation li.work a").click(function() {
$("#content #second_nav").toggle("fast");
$("#content #second_nav ul.options_3").css('display','none');
$("#content #second_nav ul.options_2").css('display','none');
$("#content #second_nav ul.options_4").css('display','none');
$('.active_2').removeClass('active_2');
$('.active').removeClass('active');
$(this).toggleClass('selected');
});

$("#content #second_nav ul.options li a#work").click(function() {
    $('.active').removeClass('active');
    $(this).attr('class','active');
    $("#content #second_nav ul.options_2").toggle("fast");
    $("#content #second_nav ul.options_4").css('display','none');
    $("#content #second_nav ul.options_3").css('display','none');
    });

$("#content #second_nav ul.options li a#writing").click(function() {
    $('.active').removeClass('active');
    $(this).attr('class','active');
    $("#content #second_nav ul.options_4").toggle("fast");
    $("#content #second_nav ul.options_3").css('display','none');
    $("#content #second_nav ul.options_2").css('display','none');
    $('.active_2').removeClass('active_2');
    });

$("#content #second_nav ul.options_2 li a#collage").click(function() {
    $('.active_2').removeClass('active_2');
    $('ul.options_3').css('display','none');
    $(this).attr('class','active_2');
    $("#content #second_nav ul.options_3#collage").toggle("fast");
    });

$("#content #second_nav ul.options_2 li a#painting").click(function() {
    $('.active_2').removeClass('active_2');
    $('ul.options_3').css('display','none');
    $(this).attr('class','active_2');
    $("#content #second_nav ul.options_3#painting").toggle("fast");
    });

$("#content #second_nav ul.options_2 li a#print").click(function() {
    $('.active_2').removeClass('active_2');
    $('ul.options_3').css('display','none');
    $(this).attr('class','active_2');
    $("#content #second_nav ul.options_3#print").toggle("fast");
    });

$("#content #second_nav ul.options_2 li a#photo").click(function() {
    $('.active_2').removeClass('active_2');
    $('ul.options_3').css('display','none');
    $(this).attr('class','active_2');
    $("#content #second_nav ul.options_3#photo").toggle("fast");
    });

}); 

【问题讨论】:

  • 我们可以看看标记吗?我认为有一个更简洁的解决方案,但需要先确定。

标签: jquery jquery-1.3 performance


【解决方案1】:

出于性能考虑:

  1. 最好使用 ID 和 CLASSES,选择器中的节点尽可能少。如果您使用 ID,请不要指定标签。所以$('#myId');$('a#myId'); 快得多,因为它使用本机javascript getElementById()。如果您指定标签,那么它会采用更复杂的选择策略。此外,ID 在 DOM 中应该是唯一的,因此请使用 $("#photo"); 代替 $("#content #second_nav ul.options_2 li a#photo") 。越短越快越好。

  2. 如果您打算多次使用它们,

    缓存您选择的 DOM 元素:

    var secondNav= $("#content #second_nav");
    secondNav.toggle("fast");
    
  3. 为简洁起见,如果它们共享相同的操作,您可以在选择器中指定多个元素。例如:

    var secondNav = $("#content #second_nav");
    secondNav.toggle("fast");
    $("ul.options_3, ul.options_2, ul.options_4",secondNav).hide();
    $('.active_2').removeClass('active_2');
    $('.active').removeClass('active');
    $(this).toggleClass('selected');
    

希望这会有所帮助...

【讨论】:

  • 我会尽可能添加链接。
【解决方案2】:

有一些事情要做。想到的最明显的一个是使用变量。

var $option2 = $("#content #second_nav ul.options_2");
var $option3 = $("#content #second_nav ul.options_3");
var $option4 = $("#content #second_nav ul.options_4");

然后,您可以在任何有长选择器的地方插入变量。

$option2.css('display', 'none');
$option3.css('display', 'none');
$option4.css('display', 'none');

应该有助于提高性能、可读性和重复性。

【讨论】:

    【解决方案3】:

    $("#content #second_nav ul.options_3").css('display','none'); $("#content #second_nav ul.options_2").css('display','none');

    == $("#content #second_nav").find("ul.options_3,ul.options_2").css('display','none');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多