为什么我的jquery函数只能运行一次?

时间:2013-08-23 02:47:03

标签: jquery

这可能听起来很愚蠢,但我是一个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>

2 个答案:

答案 0 :(得分:1)

您需要删除并添加类

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

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

答案 1 :(得分:0)

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