使用php ajax无法显示gif

时间:2013-06-12 08:45:09

标签: php jquery ajax

的index.html:

<html>
<head>
    <script type="text/javascript" src = "jquery-1.10.1.js"></script>
    <script type="text/javascript" language = "javascript">
        console.log("mm");
        function swapContent(cv) {
            console.log("mmn");

            $("#myDiv").html('<img src="loader.gif" width="44" height="10" alt="loader.gif">').show();
            var url= "myphpscript.php";
            $.post(url, { contentVar: cv }, function(data) {
                $("#myDiv").html(data).show();
            });

            console.log("mmn");
        }
    </script>
</head>
<body>
    <a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con1');"> Content1 </a> &nbsp;&bull;&nbsp;
    <a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con2');"> Content2 </a> &nbsp;&bull;&nbsp;
    <a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con3');"> Content3 </a> &nbsp;&bull;&nbsp;
    <div id = "myDiv"> My Default Content 1</div>
</body>
</html>

gif已保存在与索引相同的文件夹中。 div正在动态改变,但gif'似乎被加载了。我需要延迟或什么,以及如何?或者代码还有其他问题吗?

1 个答案:

答案 0 :(得分:0)

首先,请不要在attribtue,值对中留空格,如下所示:

...width = "44" height = "10"...

让它看起来像这样:

...width="44" height="10"...

那只是风格指南:)。

再次检查您的浏览器网络面板,看看: 它正在加载gif。因为如果没有加载,就没有gif。 如果加载它找到它。如果找不到,它应该返回 200 http响应或 404

在所有现代浏览器上,您可以使用 F12 CTRL + SHIFT + i 打开开发人员面板,然后点击“网络”。