Jquery:通过AJAX加载HTML页面然后加载另一个html页面

时间:2023-02-04 20:55:47
  //when document loads
   $(document).ready(function() {

    //navigation item is clicked
     $('.nav_item').click(function() {

    //get the clicked nav items id
    var nav_item = $(this).attr("id");
    var location_to_load = nav_item + '.html';

   //load the loading html then the page
   $('#sliding_content_container').load('loading.html', null, showResponse);

   //load the page
   function showResponse(){
    $('#sliding_content_container').delay(900).load(location_to_load);
   };

   //dont refresh
   return false;
   }); 
   });

Hey, I'm learning Jquery and just wondered why I can't call a "loading page" and then the destination with a delay, it seems everything I try doesn't work for instance adding .delay on or chaining the functions..

嘿,我正在学习Jquery,只是想知道为什么我不能调用“加载页面”然后延迟目的地,似乎我尝试的一切都不起作用,例如添加.delay或链接函数..

any help will be great, I'm giving jquery a good old crack.

任何帮助都会很棒,我给jquery一个很好的旧裂缝。

thanks

谢谢

3 个解决方案

#1


2  

The delay method is used for animations.
It adds a delay item to the animation queue, delaying any animations called after it.
It will not affect normal methods.

延迟方法用于动画。它会向动画队列添加延迟项,延迟在其后调用的任何动画。它不会影响正常方法。

Instead, you should call the setTimeout function, like this:

相反,你应该调用setTimeout函数,如下所示:

$('#sliding_content_container').load('loading.html', null, function() {
    setTimeout(function() { 
        $('#sliding_content_container').load(location_to_load);
    }, 900);
});

#2


0  

For delay to work, you need to use the queue for your next call to load in showResponse().

要使延迟起作用,您需要使用队列进行下一次调用以加载showResponse()。

function showResponse(){
    $('#sliding_content_container')
      .delay(900)
      .queue(
        function()
        {
          var $this = $(this);
          $this
            .load(
              location_to_load,
              false,
              function()
              {
                $this.dequeue();
              }
            );
        }
      );
};

If you want to, you can also use a queue other than 'fx', in case it bugs you that this queue is usually used for animations.

如果你愿意,你也可以使用“fx”之外的队列,以防你知道这个队列通常用于动画。

#3


-1  

It's normal, that you cannot delay it with .delay(); AJAX is asynchronous by default (sometimes it helps to check what abbreviation of technology means). It's quite a lot of time since I used jQuery, but as far as i remember .ajax request should be suitable. http://api.jquery.com/jQuery.ajax/

这是正常的,你不能用.delay()来延迟它。默认情况下,AJAX是异步的(有时它有助于检查技术的缩写是什么意思)。自从我使用jQuery以来已经有相当多的时间,但据我记得.ajax请求应该是合适的。 http://api.jquery.com/jQuery.ajax/

#1


2  

The delay method is used for animations.
It adds a delay item to the animation queue, delaying any animations called after it.
It will not affect normal methods.

延迟方法用于动画。它会向动画队列添加延迟项,延迟在其后调用的任何动画。它不会影响正常方法。

Instead, you should call the setTimeout function, like this:

相反,你应该调用setTimeout函数,如下所示:

$('#sliding_content_container').load('loading.html', null, function() {
    setTimeout(function() { 
        $('#sliding_content_container').load(location_to_load);
    }, 900);
});

#2


0  

For delay to work, you need to use the queue for your next call to load in showResponse().

要使延迟起作用,您需要使用队列进行下一次调用以加载showResponse()。

function showResponse(){
    $('#sliding_content_container')
      .delay(900)
      .queue(
        function()
        {
          var $this = $(this);
          $this
            .load(
              location_to_load,
              false,
              function()
              {
                $this.dequeue();
              }
            );
        }
      );
};

If you want to, you can also use a queue other than 'fx', in case it bugs you that this queue is usually used for animations.

如果你愿意,你也可以使用“fx”之外的队列,以防你知道这个队列通常用于动画。

#3


-1  

It's normal, that you cannot delay it with .delay(); AJAX is asynchronous by default (sometimes it helps to check what abbreviation of technology means). It's quite a lot of time since I used jQuery, but as far as i remember .ajax request should be suitable. http://api.jquery.com/jQuery.ajax/

这是正常的,你不能用.delay()来延迟它。默认情况下,AJAX是异步的(有时它有助于检查技术的缩写是什么意思)。自从我使用jQuery以来已经有相当多的时间,但据我记得.ajax请求应该是合适的。 http://api.jquery.com/jQuery.ajax/