doutong7216 2018-08-03 21:27
浏览 63
已采纳

在AJAX响应之后,翻转插件和javascript函数无法正常工作

I'm using Flip plugin to create a div for each row of a MySQL table and bring results via AJAX, on div front I show the name and show some information on the div back. This is my code:

index.html

<div class="result"></div>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script>
<script>
   $.post("result.php",{

    }).done(function(resp){
      $(".result").html(resp);
    });

   $(".fff").flip({
      trigger: 'manual'
   });

   function flip_back(idx){
      $(".card_"+idx).flip(true);
   }

   function flip_front(idx){
     $(".card_"+idx).flip(false);
   } 
</script>

result.php

<?php 
  $connect = mysqli_connect("localhost", "root", "mypass", "mydatabase");

  $sql = "SELECT * FROM users";
  $res = mysqli_query($connect,$sql);

  $html='';

  if(mysqli_num_rows($res)>0){
     while($row=mysqli_fetch_array($res,MYSQLI_ASSOC)){
        $id= $row['id'];

         $html.='<div class="card_'.$id.' fff"> 
            <div class="front">'.$row['name'].'
             <button onclick="flip_back(\''.$id.'\');">MORE INFO</button>            
            </div> 
            <div class="back">'.$row['email'].'Back content
            <button onclick="flip_front(\''.$id.'\');">RETURN</button>
            </div></div>';
      }

   echo $html;
  }

?>

The problem is Flip plugin and onclick javascript functions are not working, I also tried using this method:

$.post("result.php",{

    }).done(function(resp){
        $(".result").html(resp);
   });  

    $(document).on("load",function(){
        $(".fff").flip({
            trigger: 'manual'
        }); 

        function flip_back(idx){
         $(".card_"+idx).flip(true);
       }

       function flip_front(idx){
        $(".card_"+idx).flip(false);
       } 
    });

Or this:

$(document).on("load",function(){
   $.post("test2.php",{

    }).done(function(resp){
         $(".result").html(resp);
    });
 });


 $(".fff").flip({
      trigger: 'manual'
 }); 

 function flip_back(idx){
    $(".card_"+idx).flip(true);
  }

  function flip_front(idx){
    $(".card_"+idx).flip(false);
  } 

I also checked this question, and I tried this :

$(".fff").flip({
      trigger: 'manual',
      onBefore: function(){
            console.log('before starting the animation');

            $.post("result.php",{

            }).done(function(resp){
                  $(".result").html(resp);
            });

      }
}); 

function flip_back(idx){
    $(".card_"+idx).flip(true);
}

function flip_front(idx){
    $(".card_"+idx).flip(false);
} 

But it's still not working. How can I solve it?

I'd like your help.

  • 写回答

1条回答 默认 最新

  • dqm83011 2018-08-03 22:31
    关注

    Finally it works putting flip function in AJAX response:

    $.post("result.php",{
    
    }).done(function(resp){
      $(".result").html(resp);
      $(".fff").flip({
        trigger: 'manual'
      });
    });
    
    function flip_back(idx){
      $(".card_"+idx).flip(true);
    }
    
    function flip_front(idx){
      $(".card_"+idx).flip(false);
    } 
    

    If you have another alternative It would be great.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败