在使用Ajax加载外部文件,为什么在提示之后才加载完成?

使用js来加载外部文件,但是在提示之后才会加载文件,而不是在加载之后显示加载成功。为什么呢?

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>

    <script>

        $(document).ready(function () {

            $("button").click(function(){

                $("#div1").load("text/text",function (responseTxt,statusTxt,xhr) {

                        if (statusTxt=="success")

                            alert("加载成功!");

                        if(statusTxt=="error")

                            alert("加载失败:"+xhr.status+" "+xhr.statusText);

                });

            });

        });

    </script>

</head>

<body>

    <div id="div1"></div>

    <button id="btn">点击加载外部文件</button>

</body>


ibeautiful
浏览 572回答 1
1回答

缥缈止盈

原因是jQuery load方法的实现是先填充DOM再调用回调函数。下面是jQuery load方法的源码:jQuery.fn.load = function( url, params, callback ) {&nbsp; &nbsp; if ( typeof url !== "string" && _load ) {&nbsp; &nbsp; &nbsp; &nbsp; return _load.apply( this, arguments );&nbsp; &nbsp; }&nbsp; &nbsp; var selector, response, type,&nbsp; &nbsp; &nbsp; &nbsp; self = this,&nbsp; &nbsp; &nbsp; &nbsp; off = url.indexOf(" ");&nbsp; &nbsp; if ( off >= 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; selector = url.slice( off, url.length );&nbsp; &nbsp; &nbsp; &nbsp; url = url.slice( 0, off );&nbsp; &nbsp; }&nbsp; &nbsp; // If it's a function&nbsp; &nbsp; if ( jQuery.isFunction( params ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // We assume that it's the callback&nbsp; &nbsp; &nbsp; &nbsp; callback = params;&nbsp; &nbsp; &nbsp; &nbsp; params = undefined;&nbsp; &nbsp; // Otherwise, build a param string&nbsp; &nbsp; } else if ( params && typeof params === "object" ) {&nbsp; &nbsp; &nbsp; &nbsp; type = "POST";&nbsp; &nbsp; }&nbsp; &nbsp; // If we have elements to modify, make the request&nbsp; &nbsp; if ( self.length > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; jQuery.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if "type" variable is undefined, then "GET" method will be used&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "html",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: params&nbsp; &nbsp; &nbsp; &nbsp; }).done(function( responseText ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Save response for use in complete callback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = arguments;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.html( selector ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If a selector was specified, locate the right elements in a dummy div&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Exclude scripts to avoid IE 'Permission Denied' errors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery("<div>").append( jQuery.parseHTML( responseText ) ).find( selector ) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Otherwise use the full result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseText );&nbsp; &nbsp; &nbsp; &nbsp; }).complete( callback && function( jqXHR, status ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.each( callback, response || [ jqXHR.responseText, status, jqXHR ] );&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; return this;};你传入的文件路径text/text最终仍然是通过ajax去获取的,获取了文件内容后,在done方法里使用.html()把内容放到了DOM元素中,然后调用complete函数,而传入参数中的回调函数callback是在complete函数中调用的。所以,是先调用的self.html(data),渲染好了DOM,然后再调用回调函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript