dongliehuan3925 2015-12-08 09:40
浏览 32
已采纳

如何从同一div中的外部页面加载链接

Need help..

I have an index.php page. On that page, I have a link to page-2.php.

On page-2.php,

i also have a link to other page. May I know how to load the link on page-2.php in a same div on index.php page.

I'm using jQuery load method do to this. I'm able to load page-2.php inside a div on index.php. Below are the codes that I made.

$(document).ready(function() {
 $('a#staff_info').click(function(e){ //link to page-2.php
  e.preventDefault();
  var page = $(this).attr('href');
  $('#content').load(page);
  $('a#upd_staff').click(function(){ //link on page2.php 
     var page_info = $('a#upd_staff').attr('href');
     $('#content').load(page_info);
  });   
 }); 
});

the above code will direct me to a new page not on a same div

  • 写回答

3条回答 默认 最新

  • 普通网友 2015-12-08 10:02
    关注

    Ajax is asynchronous.

    $('#content').load(page);
    

    The above starts loading page two

    $('a#upd_staff').click(function(){ //link on page2.php 
    

    Then you try to bind your event handler to content from page 2 before it is has finished loading.

    So when you click on the link, there is no JS bound to it, so it acts as a normal link.


    Use deferred event handlers (so they listen on the document and check what element was clicked when the click happens).

    $(document).ready(function() {
        function ajaxLoad(event) {
            event.preventDefault();
            var page = $(this).attr('href');
            $('#content').load(page);       
        }
        $(document).on("click", "a#staff_info, a#upd_staff", ajaxLoad);
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式