问题
I currently have a (fairly sizable) JavaScript file which is used to animate some elements on a page. There are four 'sets' of scripts used on the page, each set contains a number of scripts equal to the number of steps in the tutorial on the page. What I would like to do is rework the scripts so that they will use wildcard targeting (if possible) instead of using the current setup (which is one script per function per step). I will provide the first script from each of the sets:
First Example
/* ToC List Togglers */
jQuery(document).ready(function() {
jQuery('#tutorial-toc-step-01').click(function() {
if ( jQuery('.tutorial-glyph-check-step-01').hasClass('glyphicon-unchecked') && !jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-unchecked'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-check'),
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-down'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-up'),
jQuery('#tutorial-title-reset-step-01').removeClass('hidden');
}
else if ( jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-unchecked'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-check'),
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-up'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-down'),
jQuery('#tutorial-title-reset-step-01').removeClass('hidden');
}
else if ( !jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-down'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-up');
}
else {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-up'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-down');
}
});
});
Second Example
/* Step Panel Togglers */
jQuery(document).ready(function() {
jQuery('#tutorial-title-title-step-01').click(function() {
if ( jQuery('.tutorial-glyph-check-step-01').hasClass('glyphicon-unchecked') && !jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-unchecked'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-check'),
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-down'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-up'),
jQuery('#tutorial-title-reset-step-01').removeClass('hidden');
}
else if ( jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-unchecked'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-check'),
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-up'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-down'),
jQuery('#tutorial-title-reset-step-01').removeClass('hidden');
}
else if ( !jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-down'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-up');
}
else {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-up'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-down');
}
});
});
Third Example
/* Chevron Togglers */
jQuery(document).ready(function() {
jQuery('#tutorial-title-chevron-step-01').click(function() {
if ( !jQuery('#tutorial-body-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-down'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-up');
}
else {
jQuery('.tutorial-glyph-chevron-step-01').removeClass('glyphicon-chevron-up'),
jQuery('.tutorial-glyph-chevron-step-01').addClass('glyphicon-chevron-down');
}
});
});
Fourth Example
/* Reset Togglers */
jQuery(document).ready(function() {
jQuery('#tutorial-title-reset-step-01').click(function() {
if ( jQuery('.tutorial-glyph-check-step-01').hasClass('glyphicon-check') && jQuery('#tutorial-step-01').hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-check'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-unchecked');
}
else {
jQuery('.tutorial-glyph-check-step-01').removeClass('glyphicon-check'),
jQuery('.tutorial-glyph-check-step-01').addClass('glyphicon-unchecked');
}
});
});
As you can likely guess, this current setup gets a bit cumbersome on pages that have more than a few steps (and almost unworkable on a page with dozens of steps). If possible, I would like to rework these scripts so that they will work regardless of the number of steps. As you can tell from the scripts, the steps and their elements are systematically and thoroughly id'ed and classed, which should help to some extent. If necessary, the HTML can be edited to include fid's or other attributes/elements to make the jQuery more functional or easier to use.
For reference purposes, here is a page which is actually using these scripts: http://wordpress.omnifora.com/tutorials/create-a-button-to-change-the-font-on-your-site/.
Any help/suggestions will be greatly appreciated.
回答1:
I'll give you an example for the first block provided. There you have #tutorial-toc-step-01
. So, give all similar elements a class, for example, tutorial-toc-step-c
as well as save the number in additional field, for example, data-stepnum='01'
. Then, the beginning of the code would like like that:
/* ToC List Togglers */
jQuery(document).ready(function() {
jQuery('.tutorial-toc-step-c').click(function() {
var stepnum = $(this).data('stepnum');
if ( jQuery('.tutorial-glyph-check-step-'+stepnum).hasClass('glyphicon-unchecked') && !jQuery('#tutorial-body-step-'+stepnum).hasClass('in') ) {
jQuery('.tutorial-glyph-check-step-'+stepnum).removeClass('glyphicon-unchecked'),
jQuery('.tutorial-glyph-check-step-'+stepnum).addClass('glyphicon-check'),
...
I think, the idea is understandable. You'll have to rewrite each of those four blocks in a similar way.
By the way, was it intended that the first and the second code is absolutely the same except the clicked element id
?
来源:https://stackoverflow.com/questions/20292203/rework-jquery-scripts-to-utilize-wildcard-targeting-of-classes-ids