AJAX / JQuery加载整个页面而不是div

时间:2012-07-09 00:44:45

标签: jquery ajax

我开始学习移动应用程序开发,我已经遇到了障碍。我有以下文件:

1. android.html
2. index.html
3. android.js

android.html充当加载页面,并在页面加载时将index.html链接拉入其中。当点击一个链接时,它只是意味着将#content div中的远程页面数据加载到android.html的#container div中。但这似乎对我不起作用。

以下是代码:

android.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Jonathan Stark</title>
        <meta name="viewport" content="user-scalable=no, width=device-width" />
        <link rel="apple-touch-icon-precomposed" href="myCustomIcon.png" />
        <link rel="stylesheet" href="css/android.css" type="text/css" media="screen" />
        <script src="cordova-1.6.1.js" type="text/javascript"></script>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="android.js"></script>
    </head>
    <body>
        <div id="header"><h1>Test</h1></div>
        <div id="container"></div>
    </body>
</html>

的index.html:

<html>
    <head>
        <title>Jonathan Stark</title>
        <meta name="viewport" content="user-scalable=no, width=device-width" />
        <link rel="stylesheet" type="text/css" href="android.css" media="only screen and (max-width: 480px)" />
        <link rel="stylesheet" type="text/css" href="desktop.css" media="screen and (min-width: 481px)" />
        <!--[if IE]>
            <link rel="stylesheet" type="text/css" href="explorer.css" media="all" />
        <![endif]-->
        <script src="cordova-1.6.1.js" type="text/javascript"></script>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="android.js"></script>
    </head>
    <body>
        <div id="container">
            <div id="header">
                <h1><a href="./">Jonathan Stark</a></h1>
                <div id="utility">
                    <ul>
                        <li><a href="about.html">About</a></li>
                        <li><a href="blog.html">Blog</a></li>
                        <li><a href="contact.html">Contact</a></li>
                    </ul>
                </div>
                <div id="nav">
                    <ul>
                        <li><a href="consulting-clinic.html">Consulting Clinic</a></li>
                        <li><a href="on-call.html">On Call</a></li>
                        <li><a href="development.html">Development</a></li>
                        <li><a href="http://www.oreilly.com">O'Reilly Media, Inc.</a></li>
                    </ul>
                </div>
            </div>
            <div id="content">
                <h2>About</h2>
                <p>Jonathan Stark is a web developer, speaker, and author. His consulting firm, Jonathan Stark Consulting, Inc., has attracted clients such as Staples, Turner Broadcasting, and the PGA Tour. ...</p>
            </div>
            <div id="sidebar">
                <img alt="Manga Portrait of Jonathan Stark" src="jonathanstark-manga-small.png">
                <p>Jonathan Stark is a mobile and web application developer who the Wall Street Journal has called an expert on publishing desktop data to the web.</p>
            </div>
            <div id="footer">
                <ul>
                    <li><a href="services.html">Services</a></li>
                    <li><a href="about.html">About</a></li>
                    <li><a href="blog.html">Blog</a></li>
                </ul>
                <p class="subtle">Jonathan Stark Consulting, Inc.</p>
            </div>
        </div>
    </body>
</html>

我几乎把它当作所有链接/远程页面的基页。只需更改上面#content div中的h2标签标题。

android.js:

   $(document).ready(function() {
    loadPage();

});

function loadPage(url){
    if (url == undefined){

        $('#container').load('index.html #header ul',hijackLinks);

    }
    else{


    $('#container').load(url +  '#content',hijackLinks);    

    }


}

function hijackLinks(){

    $('#container').click(function(e) {

        e.preventDefault();

        loadPage(e.target.href);
    }); 


}

有人能够告诉我为什么上面的代码显示整个页面,当我点击链接时它意味着只抓住远程页面的#content div?

谢谢!

1 个答案:

答案 0 :(得分:4)

http://api.jquery.com/load/

如果字符串中包含一个或多个空格字符,则假定第一个空格后面的字符串部分是确定要加载的内容的jQuery选择器。

换句话说,在第二次url调用中,您需要#content.load()之间的空格,或者jQuery认为它是网址的一部分。

变化:

$('#container').load(url +  '#content',hijackLinks);

要:

$('#container').load(url +  ' #content',hijackLinks);

此外,应在文档就绪回调中调用您的单击侦听器。传递给加载回调的函数实际上是一个success回调,在ajax调用成功时执行。当然,它仍然在文档就绪时被调用,因为它是在第一次加载完成时运行的,但是这种方式并不是很清楚,也没有必要把它放在那里。您可以从hijackLinks的来电中删除.load()

$(document).ready({ hijackLinks(); loadPages(); });

P.S。如果应在点击链接上执行hijackLinks,请将.click方法中的选择器从#container更改为#container a