dongyan1993 2015-12-26 23:49
浏览 17
已采纳

其他ajax中的Ajax请求

So i made an ajax request for get my "home" page and my "about" page in a "container" on click menu link button inside my index.php, and now i have three link in my "home" page, and i also want open each of these links inside my div "container" to replace the "home" page, so how can i make an ajax request after the first ajax call ?

This is my php request in the index.php:

<div id="container">
   <?php
    $d="contenu/";
    if(isset($_GET['p'])){
    $p=strtolower($_GET['p']);
    if(preg_match("/^[a-z0-9\-]+$/",$p) && file_exists($d.$p.".html")){
        include $d.$p.".html";
    }
        else{
            include "pages/404.html";
        }
        }
            else{
            include "pages/home.html";
        }
   ?>
</div>

In here my ajax:

$(document).ready(function(){
    $("#menu a").click(function(){
        page=$(this).attr("href");
        $.ajax({
            url: "pages/"+page,
            cache:false,
            success:function(html){
                afficher(html);
            },
            error:function(XMLHttpRequest,textStatus, errorThrown){
                afficher("erreur lors du chagement de la page");
            }
        });
        return false;
    });
    
});

function afficher(data){
$("#container").fadeOut(500,function(){
    $("#container").empty();
    $("#container").append(data);
    $("#container").fadeIn(1200);
    
});
}

and finally my home.html ( i just show you the links ):

<div class="section vs-section" data-speed="0.4">
            <div class="vs-transform title " data-speed="0.4"><a href="projet1.html"><h3>projet1</h3></a></div>
            <div class="vs-transform title" data-speed="0.38"><a href="projet2.html"><h3>Projet2</h3></a></div>
            <div class="vs-transform title" data-speed="0.4"><a href="projet3.html"><h3>projet3</h3></a></div>

  </div>

</div>
  • 写回答

1条回答 默认 最新

  • dongli1887 2015-12-27 00:21
    关注

    Yes you can, you should just use event delegation on() to deal with the new HTML tags added dynamically to the DOM by the first ajax request :

    $('body').on('click', '.vs-transform a', function(){
         //Second ajax request
    })
    

    Hope this helps.

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了