Switch case with conditions

前端 未结 8 1679
情深已故
情深已故 2020-12-01 02:36

Am I writing the correct switch case?

var cnt = $(\"#div1 p\").length;
                alert(cnt);
                switch (cnt) {
                    case (c         


        
8条回答
  •  [愿得一人]
    2020-12-01 03:35

    Something I came upon while trying to work a spinner was to allow for flexibility within the script without the use of a ton of if statements.

    Since this is a simpler solution than iterating through an array to check for a single instance of a class present it keeps the script cleaner. Any suggestions for cleaning the code further are welcome.

    $('.next').click(function(){
            var imageToSlide = $('#imageSprite'); // Get id of image
    
            switch(true) {
                case (imageToSlide.hasClass('pos1')):
                    imageToSlide.removeClass('pos1').addClass('pos2');
                    break;
                case (imageToSlide.hasClass('pos2')):
                    imageToSlide.removeClass('pos2').addClass('pos3');
                    break;
                case (imageToSlide.hasClass('pos3')):
                    imageToSlide.removeClass('pos3').addClass('pos4');
                    break;
                case (imageToSlide.hasClass('pos4')):
                    imageToSlide.removeClass('pos4').addClass('pos1');
            }
        }); ` 
    

提交回复
热议问题