代码之家  ›  专栏  ›  技术社区  ›  dennismonsewicz

jQuery选择器问题

  •  1
  • dennismonsewicz  · 技术社区  · 14 年前

    我有一个关于jquery选择器的快速问题。

    正在执行此操作:

    $('.class_el_1, .class_el_2').hide();
    

    就像使用jquery循环遍历每个元素一样。每个函数?

    1 回复  |  直到 14 年前
        1
  •  4
  •   Nick Craver    14 年前

    它也一样 影响 把他们都藏起来,但内部却不完全一样,不。 .each() 接受一个回调,其中 this 可以用来做特定的事情 每个 元素,所以它可以做更多的工作。 .hide() 在一个链子里 display: none; 在元素上(存储它们以前的值)。

    You can see how it works internally here ,对于没有参数的呼叫:

    for ( var i = 0, l = this.length; i < l; i++ ) {
      var old = jQuery.data(this[i], "olddisplay");
      if ( !old && old !== "none" ) {
        jQuery.data( this[i], "olddisplay", jQuery.css( this[i], "display" ) );
      }
    }
    
    // Set the display of the elements in a second loop
    // to avoid the constant reflow
    for ( var j = 0, k = this.length; j < k; j++ ) {
      this[j].style.display = "none";
    }
    

    在上面 引用元素集 $('.class_el_1, .class_el_2') 匹配,只使用 for 循环阅读。