首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的jquery函数只工作一次?

为什么我的jquery函数只工作一次?
EN

Stack Overflow用户
提问于 2013-08-23 02:47:02
回答 2查看 300关注 0票数 0

这听起来可能很愚蠢,但我是一个Jquery,当我在手机上左右滑动时,我试图做一个div标记来调整大小,而我不知道为什么它只执行一次?这里的jquery专家能帮我吗!谢谢

这是代码

代码语言:javascript
复制
<!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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-23 02:58:58

您需要删除并添加这些类。

代码语言:javascript
复制
function swiperightHandler( event ){
  $( event.target ).removeClass('swipeleft').addClass( "swiperight" );
}

代码语言:javascript
复制
function swiperightHandler( event ){
  $( event.target ).removeClass('swiperight').addClass( "swipeleft" );
}
票数 1
EN

Stack Overflow用户

发布于 2013-08-23 02:53:08

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18393853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档