0

这听起来可能很愚蠢,但我是一个 Jquery 菜鸟,当我在移动设备上左右滑动时,我正在尝试制作一个 div 标签来调整大小,我不知道为什么它只执行一次?这里的任何 jquery 大师都可以帮助我吗!!!谢谢你

这是代码

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>

  <div class="box">
  </div>

  <script>
    $(function right(){
      // Bind the swiperightHandler callback function to the swipe event on div.box
      $( "div.box" ).on( "swiperight", swiperightHandler );

      // Callback function references the event target and adds the 'swiperight' class to it
      function swiperightHandler( event ){
        $( event.target ).addClass( "swiperight" );
      }
    });

    $(function left(){
      // Bind the swiperightHandler callback function to the swipe event on div.box
      $( "div.box" ).on( "swipeleft", swiperightHandler );

      // Callback function references the event target and adds the 'swiperight' class to it
      function swiperightHandler( event ){
        $( event.target ).addClass( "swipeleft" );
      }
    });
  </script>
</body>
</html>
4

2 回答 2

1

您需要删除和添加类

function swiperightHandler( event ){
  $( event.target ).removeClass('swipeleft').addClass( "swiperight" );
}

function swiperightHandler( event ){
  $( event.target ).removeClass('swiperight').addClass( "swipeleft" );
}
于 2013-08-23T02:58:58.763 回答
0

它可能被执行了不止一次,addClass当你第二次调用它时不会做任何事情,因为它已经将该类添加到元素中!

于 2013-08-23T02:53:08.593 回答