0
 $('.dragbox').each(function(){
        $('.close').click(function(){
            $(this).parent().hide();
        }),
        $('.colpase').click(function(){
            $(this).siblings('.dragbox_content').toggle();
        })
     });    
4

5 回答 5

3

考虑到 jquery 作为一个包装集(集合)工作,我认为你不需要每个方法,只是

$('.dragbox').find('.close').click(function(){
    $(this).parent().hide();
})
$('.dragbox').find('.colpase').click(function(){
    $(this).siblings('.dragbox_content').toggle();
})

处理程序将应用于所有匹配的元素,而不需要每个。

这将在 .dragbox 项目中找到所有 .close 和 .colpase 我认为这就是你所追求的......

编辑为使用find以获得轻微的性能改进。谢谢丹/亚历克斯。

于 2010-07-05T17:22:45.023 回答
1

不。大概您想将点击处理程序应用于每个拖动框中的匹配元素。你可以这样做:

 $('.dragbox').each(function(){
        $('.close', this).click(function(){
            $(this).parent().hide();
        }),
        $('.colpase', this).click(function(){
            $(this).siblings('.dragbox_content').toggle();
        })
     });    

如果您只想全局添加处理程序,则不需要每个处理程序。

于 2010-07-05T17:18:57.030 回答
1

看起来好像不需要 each() 函数。您可能会多次将事件处理程序应用于对象。只是:

    $('.close').click(function(){
        $(this).parent().hide();
    });
    $('.colpase').click(function(){
        $(this).siblings('.dragbox_content').toggle();
    });

应该做的伎俩。

于 2010-07-05T17:21:45.253 回答
0

如果您在“每个”中将“父级”称为实际的“.dragbox”

$('.dragbox').each(function(){

  var self = this;

  $(self).find('.close').bind("click", function(){

    $(self).hide();

  }); // <-- ","?

  $(self).find('.colpase').bind("click", function(){

    //This line confuses me because you could do the same in the selector for the click event
    //unless you do have more things in the function
    $(this).siblings('.dragbox_content').toggle();

  });

  /*
  $(self).find('.colpase').siblings('.dragbox_content').bind("click", function(){
    $(this).toggle();
  });
  */

}); 
于 2010-07-05T20:41:43.263 回答
0
function set_colors(pick_color)
{
    var color_code=pick_color;
    $('#'+ color_owner).parent().css('background-color',color_code);
}
于 2010-07-05T18:25:57.730 回答