Rework jQuery Scripts to Utilize Wildcard Targeting of Classes/Ids

前端 未结 1 1697
野性不改
野性不改 2020-12-22 08:01

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

相关标签:
1条回答
  • 2020-12-22 08:35

    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?

    0 讨论(0)
提交回复
热议问题