0

http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp

我试图让上面的脚本工作,但有一些变化。我需要将内容作为 php 包含加载,而不是作为 html 字符串加载。我现在的主要问题是我认为代码没有指向正确的页面,因此没有新内容。

php:

switch($_GET['page'])  {
    case 'code' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/code.php'); break;
    case 'design' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/design.php'); break;
    case 'writing' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/writing.php'); break;

html:

<ul id="menu">
         <li><a title="index" href="#index.php" rel="ajax"><span>home</span></a></li>
         <li><a title="code" href="#code.php" rel="ajax"><span>code</span></a></li>
         <li><a title="design" href="#design.php" rel="ajax"><span>design</span></a></li>
         <li><a title="illustration" href="#illustration.php" rel="ajax"><span>illustration</span></a></li>
         <li><a title="writing" href="#writing.php" rel="ajax"><span>writing</span></a></li>
         <li><a title="links" href="#links.php" rel="ajax"><span>links</span></a></li>
         <li><a title="about" href="#about.php" rel="ajax"><span>about</span></a></li>
        </ul>

javascript:

<script type="text/javascript">

    $(document).ready(function () {

    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);   

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //for back button
        $.history.load(hash);   

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content and show the progress bar
        $('#content').hide();
        $('#loading').show();

        //run the ajax
        getPage();

        //cancel the anchor tag behaviour
        return false;
    }); 
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) getPage();    
}

function getPage() {

    //generate the parameter for the php script
    var data = 'page=' + document.location.hash.replace(/^.*#/, '');
    $.ajax({
        url: "loader.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //hide the progress bar
            $('#loading').hide();   

            //add the content retrieved from ajax and put it in the #content div
            $('#content').html(html);

            //display the body with fadeIn transition
            $('#content').fadeIn('slow');       
        }       
    });
}

    </script>
4

2 回答 2

1

include不返回任何内容,除了表示包含成功/失败的真/假。基本上它很像eval(),除了使用外部文件。如果您包含的页面实际上正在生成输出,您必须先捕获它:

ob_start();
include('somepage.php');
$content = ob_get_clean();

return $content;

或者,如果包含正在生成内容并将其保存到变量中,则:

include('somepage.php');
echo $whatever_var_that_somepage_php_stored_the_content_in;
于 2011-11-21T21:39:49.863 回答
1

你确定你走对了吗?

case 'code' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/code.php'); break;
case 'design' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/design.php'); break;
case 'writing' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/writing.php'); break;

不同环境中的 $_SERVER['DOCUMENT_ROOT'] 可能有斜杠或没有斜杠,因此在包含来自 $_SERVER['DOCUMENT_ROOT'] 的文件时要小心

case 'code' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/code.php'); break;
case 'design' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/design.php'); break;
case 'writing' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/writing.php'); break;
于 2011-11-22T02:39:18.300 回答