views:

280

answers:

1

1 set:

$('#tabs li:first-child').addClass('first-child');
$('.photos li:first-child').addClass('first-child');
$('#alphabet li:first-child').addClass('first-child');

2 set:

$(function(){
    $("#header-thumbs img").fadeTo("fast",1);
    $("#header-thumbs img").hover(function(){
     $(this).fadeTo("fast",.7)},function(){
     $(this).fadeTo("fast",1)})});
$(function(){$(".catalog dt img").fadeTo("fast",1);
    $(".catalog dt img").hover(function(){
     $(this).fadeTo("fast",.7)},function(){
     $(this).fadeTo("fast",1)})});
$(function(){$(".photos li img").fadeTo("fast",1);
    $(".photos li img").hover(function(){
    $(this).fadeTo("fast",.7)},function(){
    $(this).fadeTo("fast",1)})});

Is it possible to optimize, for a less code?


Thanks Paolo Bergantino for his help, the result is:

Optimized and packed first set (540->171):

$(function(){$("#header-thumbs, .catalog dt, .photos li").find("img").fadeTo("fast",1).hover(function(){$(this).fadeTo("fast",.7)},function(){$(this).fadeTo("fast",1)})});

second set (158->78):

$('#tabs, .photos, #alphabet').find('li:first-child').addClass('first-child');

Used Dean Edwards packer

+6  A: 

It is common practice when dealing with sets of elements and CSS/Javascript to give them all a similar class if at all possible to make them easier to work with. You could then just do this:

$('.mysimilarclass li:first-child').addClass('first-child');

If that's not an option, you could also just aggregate all the selectors into one:

$('#tabs, .photos, #alphabet').find('li:first-child').addClass('first-child');

For the second set the same thing applies. You can "group" them all with one class, or just aggregate all the selectors you want and then only find images from them. Plus, you can take advantange of jQuery's chaining to not query the DOM more than once:

$(function(){
    $("#header-thumbs, .catalog dt, .photos li")
     .find("img")
     .fadeTo("fast", 1)
     .hover(function() {
         $(this).fadeTo("fast", .7);
     }, function() {
         $(this).fadeTo("fast", 1);
     });
});

Also, if at all possible, it is good practice to always add what element is expected to have a class in your selectors. So instead of .photos li it is better to do ul.photos li or whatever element will have a class of photos. This is not always possible, of course, but it is advisable whenever it is.

Paolo Bergantino
very good answer, thanks.
Mike
is it possible to use multiple ".find"?
Mike
".find("img") .find("a")" - so? no comma needed?
Mike
It is, but it might not behave how you expect it to. Once you do the find("img") above, for example, the "scope" of the search is no longer the initial selector, but the image elements themselves.
Paolo Bergantino
If you wanted to find both images and links, it'd be better to do .find("img, a")
Paolo Bergantino
If you wanted to do multiple finds including the original selector, you would use the andSelf() function: http://docs.jquery.com/Traversing/andSelf
Paolo Bergantino
fantastic, thanks.
Mike