How to reduce the recording of JS - to normalize (short rooms)



  • Please indicate how to bring the jquery code into normal appearance:

    $('.go0').click(function(){
        carousel.trigger('owl.goTo', 0)
    });
    

    $('.go1').click(function(){
    carousel.trigger('owl.goTo', 1)
    });

    $('.go2').click(function(){
    carousel.trigger('owl.goTo', 2)
    });

    $('.go3').click(function(){
    carousel.trigger('owl.goTo', 3)
    });

    $('.go4').click(function(){
    carousel.trigger('owl.goTo', 4)
    });

    $('.go5').click(function(){
    carousel.trigger('owl.goTo', 5)
    });

    $('.go6').click(function(){
    carousel.trigger('owl.goTo', 6)
    });

    $('.go7').click(function(){
    carousel.trigger('owl.goTo', 7)
    });

    $('.go8').click(function(){
    carousel.trigger('owl.goTo', 8)
    });

    Elements.go0.go1 It could be infinitely a lot, and it's not normal to write them like that.

    Unfortunately, I didn't find a way to write this down as n+1 or something.



  • Decision "not including brains"

    for (var ix = 0; ix < 9; ix += 1) {
        $('.go' + ix).click(function (ix) {
            return function() {
                carousel.trigger('owl.goTo', ix);
            };
        }(ix));
    }
    

    Now a normal decision:

    Typically, the cruising switches have a common ancestor and the elements themselves are in a row. So it's enough to find a descendant number at the ancestor that in jQuery is a trivial task. Suppose all switches have a class. go:

    $('.go').click(function() {
        carousel.trigger('owl.goTo', $(this).index());
    });
    

    If the elements .go There's still one of them in their spine, instead. $(this) It will be necessary to indicate an ancestor whose position within its ancestor can give a slide number.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2