开发者

each function jquery. is it correct?

 $('.dragbox').each(function(){
        $('.close').click(function(){
       开发者_高级运维     $(this).parent().hide();
        }),
        $('.colpase').click(function(){
            $(this).siblings('.dragbox_content').toggle();
        })
     });    


Considering jquery works as a wrapped set (collection) i dont think you need the each method, just the

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

the handlers will be applied to all matched elements without the need for the each.

this will find all of the .close and .colpase inside of the .dragbox item(s) i assumed that is what you were after...

edited to use find in order to gain slight performance improvement. Thanks Dan/Alex.


No. Presumably you want to apply the click handlers to matching elements within each dragbox. You can do that with:

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

If you just wanted to add the handlers globally, you wouldn't want the each.


It doesn't look as if you need the each() function there. You may be applying the event handlers to the objects multiple times. Just:

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

Should do the trick.


if you're referring to "parent" to actual ". dragbox" in "each"

$('.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();
  });
  */

}); 


function set_colors(pick_color)
{
    var color_code=pick_color;
    $('#'+ color_owner).parent().css('background-color',color_code);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜