Change CSS value when page has loaded on all EXCEPT first instance
I have the following piece of code do change the value of a division once the page has loaded but I was wondering if it would be possible to amend it to change the value of all of the di开发者_开发百科visions with that class with the exception of the first one.
jQuery('#accordion div.pane').css('display', 'none');
Simply use the :gt
selector to select all but the first:
jQuery('#accordion div.pane:gt(0)').css('display', 'none');
or the less concise "notty firsty":
jQuery('#accordion div.pane:not(:first)').css('display', 'none');
and why not use hide instead of setting the style:
jQuery('#accordion div.pane:gt(0)').hide();
For better performance (and a concise solution), you might consider using JavaScript's Array.slice
, which works on jQuery objects since they are arrayish:
jQuery('div').slice(1).hide();
jQuery('#accordion div.pane').not(':first').css('display', 'none');
You could give your first element a distinguishing ID or class, and use the jQuery not selector:
http://api.jquery.com/not-selector
精彩评论