Javascript 只需要构造一次查询

Javascript 只需要构造一次查询,javascript,jquery,Javascript,Jquery,我有一个函数,它使用.slidetoggle折叠两个段落,并创建一个div“展开”。代码工作正常,但我正在尝试将其优化到不需要每次构造查询的位置。有什么建议吗 <script type="text/javascript"> $(document).ready(function(){ $(".press_release").each(function(){ if($(this).children("p").length >

我有一个函数,它使用.slidetoggle折叠两个段落,并创建一个div“展开”。代码工作正常,但我正在尝试将其优化到不需要每次构造查询的位置。有什么建议吗

    <script type="text/javascript">
    $(document).ready(function(){

        $(".press_release").each(function(){
            if($(this).children("p").length > 1){
                $('<div><a href="#readmore" class="readmore">Read More&hellip;</a></div>').appendTo($(this));

                $(this).children("p").first().addClass('first-p');
                $(this).children("p").not('.first-p').hide();

                $(this).find('.readmore').click(function(){
                    $(this).parent().siblings('p').not('.first-p').slideToggle(500);
                    return false;
                });
            }

        });
    });
</script>

$(文档).ready(函数(){
$(“.press_release”)。每个(函数(){
如果($(此).children(“p”).length>1){
$('')。附于($(本));
$(this.children(“p”).first().addClass(“first-p”);
$(this.children(“p”).not('.first-p').hide();
$(this)。查找('.readmore')。单击(函数(){
$(this).parent().sibbines('p')。not('first-p')。slideToggle(500);
返回false;
});
}
});
});
我会缓存
$(this)
引用,以避免重复创建jquery对象:

$(".press_release").each(function(){
       var myThis = $(this); 
       if(myThis.children("p").length > 1){
  ....

在整个脚本中使用缓存的引用。

正如TGH所提到的,您应该缓存任何重复的查询。在您的代码中,有三个我们关心的主要问题:单个新闻稿(
$(this)
)、第一段和其余段落

$(document).ready(function(){

  $(".press_release").each(function(){
    var $this = $(this),
      $paragraphs = $this.children("p"),
      $firstParagraph = $paragraphs.first();

    // Remove the first paragraph from our set
    $paragraphs = $paragraphs.not($firstParagraph);

    // We're only counting paragraphs after the first
    if($paragraphs.length > 0){
      $('<div><a href="#readmore" class="readmore">Read More&hellip;</a></div>').appendTo($this);

      $firstParagraph.addClass('first-p');
      $paragraphs.hide();

      // Delay the lookup of .readmore now; gain a slight benefit here at the
      // expense of a tiny cost each time something inside $this is clicked.
      // Binding directly to the .readmore element as you've done in your
      // original code likely won't have any noticeable impact either way.
      $this.on('click', '.readmore', function (){
        $paragraphs.slideToggle(500);
        return false;
      });
    }

  });
});
$(文档).ready(函数(){
$(“.press_release”)。每个(函数(){
变量$this=$(this),
$段落=$this.children(“p”),
$FIRSTPRAGEN=$PARTIGES.first();
//从我们的集合中删除第一段
$段落=$段落.非($第一段);
//我们只计算第一段之后的段落
如果($段落长度>0){
$('')。附于($本);
$firstparation.addClass('first-p');
$pages.hide();
//现在延迟.readmore的查找;在下面的
//每次点击$this内的某个内容时,只需花费很少的成本。
//直接绑定到.readmore元素,就像您在
//原始代码可能不会有任何明显的影响。
$this.on('click','.readmore',函数(){
$段落.滑动切换(500);
返回false;
});
}
});
});

Ok,这对很多人都有帮助。我没有练习缓存任何查询,但我确实看到了好处。谢谢