jquery .load()不起作用

时间:2011-02-27 19:26:12

标签: javascript jquery load

我似乎无法使jquery的.load()函数起作用。必须是简单的我缺少...基本上只是尝试将more.html的片段加载到index.html。

的index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/jquery-1.5.js" />
    <title>Test</title>

    <script type="text/javascript">
        $(document).ready(function () {
            alert("jquery script executing...");
            $('#foo').load('more.html #bar', loadComplete);
        });

        function loadComplete (response, status, xhr) {
            alert("load complete.");
        }
    </script>
</head>
<body>
    <div id="foo">
        foo foo foo
    </div>
</body>
</html>

more.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>More</title>
</head>
<body>
    <div id="bar">
        bar bar bar
    </div>
</body>

我看到两个警报 - 正在执行脚本,并且正在调用回调(即负载正在完成)。但是,内容不要改变。我尝试过safari,chrome和firefox(都在OSX上)。肯定有一些我不知道的东西......?

1 个答案:

答案 0 :(得分:4)

在jquery 1.5版本中,load函数被破坏了。您可以在http://bugs.jquery.com/ticket/8125找到错误提示。这在1.5.1版中已得到修复。您可以找到包含所有最新修补程序的最新jquery版本,包括http://code.jquery.com/jquery-git.js上的load()