Skip to main content
Code Review

Return to Answer

edited body
Source Link
user688
user688
// If you use selectors multiple times, always cache them into variables, else jQuery has to search for them multiple times.
var $cont = $('.cont'),
 $expandAll = $('#expandAll'),
 $trigger = $('.trigger');
function expandAll() {
 $cont.removeClass('hid');
 $trigger.htmltext('-');
 // Use .text() instead of .html().
 // You can chain these calls together.
 // If you use .one(), you don't have to unbind the previous event handler next time, jQuery does it for you.
 $expandAll.text('Collapse All').one('click', collapseAll);
}
function collapseAll() {
 $cont.addClass('hid');
 $trigger.htmltext('-');
 $expandAll.text('Expand All').one('click', expandAll);
}
// You can pass a function to jQuery itself – it's a shorthand for $(document).ready().
$(function() {
 // There's no need to pass the event argument, because
 // 1. You didn't use it there
 // 2. jQuery "normalizes" the event object, so you don't have to pass it as an argument. If you want a shorthand for it, do it inside the function:
 // var e = event;
 $trigger.click(function() {
 // Always use var to declare variables, to prevent polluting the global scope.
 var $this = $(this);
 // Again: use .text() instead of .html().
 $this.text($this.text() == '+' ? '-' : '+');
 // There's no need to do this:
 // $($this.nextAll(".cont").get(0)).toggleClass("hid");
 // You can simply do this:
 $this.nextAll('.cont').toggleClass('hid');
 });
 $expandAll.one('click', expandAll);
});
// If you use selectors multiple times, always cache them into variables, else jQuery has to search for them multiple times.
var $cont = $('.cont'),
 $expandAll = $('#expandAll'),
 $trigger = $('.trigger');
function expandAll() {
 $cont.removeClass('hid');
 $trigger.html('-');
 // Use .text() instead of .html().
 // You can chain these calls together.
 // If you use .one(), you don't have to unbind the previous event handler next time, jQuery does it for you.
 $expandAll.text('Collapse All').one('click', collapseAll);
}
function collapseAll() {
 $cont.addClass('hid');
 $trigger.html('-');
 $expandAll.text('Expand All').one('click', expandAll);
}
// You can pass a function to jQuery itself – it's a shorthand for $(document).ready().
$(function() {
 // There's no need to pass the event argument, because
 // 1. You didn't use it there
 // 2. jQuery "normalizes" the event object, so you don't have to pass it as an argument. If you want a shorthand for it, do it inside the function:
 // var e = event;
 $trigger.click(function() {
 // Always use var to declare variables, to prevent polluting the global scope.
 var $this = $(this);
 // Again: use .text() instead of .html().
 $this.text($this.text() == '+' ? '-' : '+');
 // There's no need to do this:
 // $($this.nextAll(".cont").get(0)).toggleClass("hid");
 // You can simply do this:
 $this.nextAll('.cont').toggleClass('hid');
 });
 $expandAll.one('click', expandAll);
});
// If you use selectors multiple times, always cache them into variables, else jQuery has to search for them multiple times.
var $cont = $('.cont'),
 $expandAll = $('#expandAll'),
 $trigger = $('.trigger');
function expandAll() {
 $cont.removeClass('hid');
 $trigger.text('-');
 // Use .text() instead of .html().
 // You can chain these calls together.
 // If you use .one(), you don't have to unbind the previous event handler next time, jQuery does it for you.
 $expandAll.text('Collapse All').one('click', collapseAll);
}
function collapseAll() {
 $cont.addClass('hid');
 $trigger.text('-');
 $expandAll.text('Expand All').one('click', expandAll);
}
// You can pass a function to jQuery itself – it's a shorthand for $(document).ready().
$(function() {
 // There's no need to pass the event argument, because
 // 1. You didn't use it there
 // 2. jQuery "normalizes" the event object, so you don't have to pass it as an argument. If you want a shorthand for it, do it inside the function:
 // var e = event;
 $trigger.click(function() {
 // Always use var to declare variables, to prevent polluting the global scope.
 var $this = $(this);
 // Again: use .text() instead of .html().
 $this.text($this.text() == '+' ? '-' : '+');
 // There's no need to do this:
 // $($this.nextAll(".cont").get(0)).toggleClass("hid");
 // You can simply do this:
 $this.nextAll('.cont').toggleClass('hid');
 });
 $expandAll.one('click', expandAll);
});
Source Link
user688
user688
// If you use selectors multiple times, always cache them into variables, else jQuery has to search for them multiple times.
var $cont = $('.cont'),
 $expandAll = $('#expandAll'),
 $trigger = $('.trigger');
function expandAll() {
 $cont.removeClass('hid');
 $trigger.html('-');
 // Use .text() instead of .html().
 // You can chain these calls together.
 // If you use .one(), you don't have to unbind the previous event handler next time, jQuery does it for you.
 $expandAll.text('Collapse All').one('click', collapseAll);
}
function collapseAll() {
 $cont.addClass('hid');
 $trigger.html('-');
 $expandAll.text('Expand All').one('click', expandAll);
}
// You can pass a function to jQuery itself – it's a shorthand for $(document).ready().
$(function() {
 // There's no need to pass the event argument, because
 // 1. You didn't use it there
 // 2. jQuery "normalizes" the event object, so you don't have to pass it as an argument. If you want a shorthand for it, do it inside the function:
 // var e = event;
 $trigger.click(function() {
 // Always use var to declare variables, to prevent polluting the global scope.
 var $this = $(this);
 // Again: use .text() instead of .html().
 $this.text($this.text() == '+' ? '-' : '+');
 // There's no need to do this:
 // $($this.nextAll(".cont").get(0)).toggleClass("hid");
 // You can simply do this:
 $this.nextAll('.cont').toggleClass('hid');
 });
 $expandAll.one('click', expandAll);
});
default

AltStyle によって変換されたページ (->オリジナル) /