简体   繁体   English

如何在jquery的.load()函数中放置两个回调函数?

[英]how can i place two callback functions in a .load() function in jquery?

I'm having issues figuring out how to handle error on my page using .load() function, i already used the call back function for transitioning and i don't know how or to place the error code and make it work correctly.... 我在弄清楚如何使用.load()函数处理页面错误时遇到问题,我已经使用了回调函数进行转换,而且我不知道如何或放置错误代码并使之正常工作。 ..

i av this code..... 我这个代码.....

$('.menuLink:eq(0)').click(function(event) {
    event.preventDefault();
    setTimeout($('#navigation ul li').removeClass('expand', 'normal'), 1000);
    $('section').hide().load('index.html section', function() {
        $(this).fadeIn('slow');
    });
});

I'll like to load any error that may occur in the section tag... 我想加载任何可能在section标签中发生的错误...

If you are using load() you can do the error checking in the same callback function. 如果使用load() ,则可以在同一回调函数中进行错误检查。 For example, as given in the JQuery documentation : 例如,如JQuery文档中所述

Display a notice if the Ajax request encounters an error. 如果Ajax请求遇到错误,则显示通知。

<script>
$( "#success" ).load( "/not-here.php", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});
</script>

In that callback function, first check for errors. 在该回调函数中,首先检查错误。 Then, if there are no errors, do the fadeIn. 然后,如果没有错误,请执行fadeIn。

$('.menuLink:eq(0)').click(function(event) {
event.preventDefault();
setTimeout($('#navigation ul li').removeClass('expand', 'normal'), 1000);
$('section').hide().load('index.html section', function() {
    // Check for errors here
    $(this).fadeIn('slow');
});

}); });

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

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