2011-01-09 107 views
-1
$(".refine_button").click(function(){ 
var butt = $(this); 
$(this).next(".refine_block").slideToggle("normal", function(){ 
     butt.children(".refine_icon").toggleClass("opened"); 
     }); 
}); 

特别地,“butt.children”这段代码在简单英语中意味着什么?

由于

+0

这里有一个提示:前两行`butt.children`,有启动`VAR对接=`线。 – 2011-01-09 22:42:36

+0

什么是上下文? – 2011-01-09 22:42:39

+1

我很认真!我在google上找不到关于butt.children做了什么的任何内容,但是如果我删除它,我的代码无法正常工作。具体而言,当单击一个.refine_button时,页面上的每个.refine_icon元素都会将该类打开。 – Anthony 2011-01-09 23:17:45

回答

2
$(".refine_button").click(function(){ // when an element with the class refine_button is clicked 
    var butt = $(this); // store a reference to the element clicked 
    $(this).next(".refine_block") // get the next element if it has a class refine_block 
     .slideToggle("normal", function(){ // and slide it open or closed, depending on its current status 
      // when that animation is finished 
      butt.children(".refine_icon") // get any children of the original clicked element that have the class refine_icon 
       .toggleClass("opened"); // and add or remove the class opened, depending whether they currently have it 
     }); 
}); 
2

与类被点击会发现元件以一类“refine_block”,要么“滑动”“refine_button”的任何按钮它进入或退出(取决于它是否已经进入或退出,它会调用相反的)。当幻灯片完成时,它会将一个类添加到受影响的“refine_block”中名为“opened”的“refine_icon”元素中。这只会影响直接与被点击的原始“refine_button”相邻的代码中的下一个元素。

0

butt是一个包含jQuery对象的变量;其中包含一个元素(被单击的类为refine-button的元素)

children函数返回jQuery对象中的元素的子元素,可选地通过选择器进行过滤,以便检索子元素的子元素(并且顺便说一句,您正在切换“已打开”的类)

0

当您单击应用了“refine_button”类的元素时,jQuery会查找下一个应用了“refine_block”类的页面元素,并折叠或展开它(通过slideToggle方法)。一旦slideToggle完成,它将添加或移除所有“refine_icon”元素上的“打开”类在“re”中定义fine_block”。

0

问题的兴趣:

  1. 这将适用于任何元素与refine_button类,不只是按钮 - 甚至div或图像。如果一只狗被称为“Kitty”,它仍然是一只狗。
  2. children将只返回直接父元素的子元素,不深查找。
  3. butt被声明为保留父元素,因为$(this)将在slideToggle函数内部具有不同含义。