简体   繁体   English

Ajax帮助加载内容

[英]Ajax help loading content

Hi I got a problem while using this code (which is in a js named ajax.js) 嗨,我在使用此代码时遇到问题(在名为ajax.js的js中)

    $(function(){
      $("#loading").hide();

         $("ul#nav a").click(function(){
            page = "content/"+$(this).attr('href')

            $("#loading").ajaxStart(function(){
               $(this).show()
               })
            $("#loading").ajaxStop(function(){
               $(this).hide();

            })

            $(".content").load(page);
            return false;
         })
     })

On the index.php first on the head division I include the scripts 在index.php上,首先在头部分添加脚本

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>

And on body division,this is the main menu navigation 在身体分割上,这是主菜单导航

<ul id="nav">
  <li><a href="page1.php">Here 1</a></li>
  <li><a href="page2.php">Here 2 </a></li>
</ul>

And here is the code where the content is loaded 这是加载内容的代码

<div class="content">
  <img src="imgs/ajax-loader.gif" width="16" height="16" alt="Loading..." id="loading" />
  <?php include('content/page1.php'); ?>
</div>

This code is working perfectly.But my problem is that inside page1.php I got 这段代码运行正常。但是我的问题是在page1.php里面

<ul id="nav">
  <li><a href="page3.php">Here 3 </a></li>
  <li><a href="page4.php">Here 4 </a></li>
</ul>

and the whole system is not working properly. 并且整个系统无法正常运行。 If i use the links from the main navigation bar its ok but if i try to use links from any of the other pages its not working. 如果我使用主导航栏中的链接,可以,但是如果我尝试使用其他任何页面上的链接,则无法正常工作。 I tried to load the ajax script on every page.php but still is not working. 我试图在每个page.php上加载ajax脚本,但仍然无法正常工作。 Any ideas? 有任何想法吗?

OK the issue with the click event binding. 单击与单击事件绑定有关的问题。

You need to bind the click event on nav a because of the page1.php have nav menu so the content is loaded using the ajax but click event is not bind on new menu item. 您需要在nav a上绑定click事件,因为page1.php具有nav菜单,因此使用ajax加载了内容,但是click事件未绑定到新菜单项上。

So create new function called BindClickEvent 因此,创建名为BindClickEvent的新函数

function BindClickEvent(){
        $("ul#nav a").unbind( "click" );
        $("ul#nav a").bind("click", function(){
            page = "content/"+$(this).attr('href')

            $("#loading").ajaxStart(function(){
               $(this).show()
               })
            $("#loading").ajaxStop(function(){
               $(this).hide();

            })

            $(".content").load(page);
            return false;
         })
}

call BindClickEvent function in page load and page1.php file content also So whenever you will add / remove menu call BindClickEvent function in ajax call response. BindClickEvent可以在页面加载和page1.php文件内容中调用BindClickEvent函数,因此,每当您要添加/删除菜单时,都可以在ajax调用响应中调用BindClickEvent函数。

It is the issue of delegated function, just replace 这是委托函数的问题,只需替换

$("ul#nav a").click(function(){

with

$(document).on('click', 'ul#nav a', function(){

your problem will be solved :) 您的问题将得到解决:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM