无法更改我的ajax内容的代码

时间:2015-07-08 02:26:59

标签: javascript php jquery ajax

Sup,对,我正在使用Tutorialzine中的这个脚本。 基本上,链接的基本设置是:

/page_number.php

并且链接本身是;页面编号

但我无法链接除page_number.php以外的任何内容,我需要知道如何更改脚本以便我可以链接到我网站上的任何链接而不会弄乱它。

这是代码;

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<link rel="stylesheet" type="text/css" href="demo.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>

</head>

<body>

<a href="#page1">Page 1</a>
<a href="#page2">Page 2</a>
<a href="#page3">Page 3</a>
<a href="#page4">Page 4</a>



    <div id="pageContent">
    Hello, this is a demo for a <a href="http://tutorialzine.com/2009/09/simple-ajax-website-jquery/" target="_blank">Tutorialzine tutorial</a>. To test it, click some of the buttons above. Have a nice stay!</div>
    </div>


</body>
</html>

的script.js

var default_content="";

$(document).ready(function(){

    checkURL();
    $('ul li a').click(function (e){

            checkURL(this.hash);

    });

    //filling in the default content
    default_content = $('#pageContent').html();


    setInterval("checkURL()",250);

});

var lasturl="";

function checkURL(hash)
{
    if(!hash) hash=window.location.hash;

    if(hash != lasturl)
    {
        lasturl=hash;

        // FIX - if we've used the history buttons to return to the homepage,
        // fill the pageContent with the default_content

        if(hash=="")
        $('#pageContent').html(default_content);

        else
        loadPage(hash);
    }
}


function loadPage(url)
{
    url=url.replace('#page','');

    $('#loading').css('visibility','visible');

    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "html",
        success: function(msg){

            if(parseInt(msg)!=0)
            {
                $('#pageContent').html(msg);
                $('#loading').css('visibility','hidden');
            }
        }

    });

}

load_page.php

<?php

if(!$_POST['page']) die("0");

$page = (int)$_POST['page'];

if(file_exists('page_'.$page.'.php'))
echo file_get_contents('page_'.$page.'.php');

else echo 'There is no such page!';
?>

任何帮助都会非常感激......

1 个答案:

答案 0 :(得分:0)

根据@ epascarello的评论,您需要绑定您的函数,通过执行以下操作将该点链接到哈希:

// bind to <a> that have their href attribute start with #
$('ul li a[href^=#]').click(function (e) { 
    checkURL(this.hash);
});