0

ここでスクリプトに問題があり、その理由がわかりません。多くのテストを行った結果、主な問題は Ajax リクエストにあることがわかりました。また、スクリプトの別のバージョンで Ajax リクエストを使用しています...当時、コンテンツの変更は非常にうまくいきました...しかし、今は戻るボタンを有効にしたかったので、それがどのように機能するかをここで尋ねました。 ..私は非常に良い答えを得たので、このチュートリアルの助けを借りてそれをやろうとしました が、結果は次のとおりです。URLの変更が機能し、古いコンテンツはフェードアウトしていますが、新しいコンテンツ(からリクエスト) が更新されていません... 更新: リクエストを送信していないようなものです...

よろしくお願いします

そして私の下手な英語でごめんなさい。

JavaScript:

$(function () {

    var newHash = "",
        $content = $("#content"),
        $el;

    $('ul li a').click(function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function () {

        newHash = window.location.hash.substring(1);
        url=$(this).attr("href");
        if (newHash) {
            $content

            .fadeOut(200, function () {

                //                 url=url.replace('#','');

                $('#loading').css('visibility', 'visible'); //show the rotating gif animation

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

                        if (parseInt(msg) != 0) //if no errors
                        {
                            $('#content').html(msg); //load the returned html into pageContet

                        }
                        $('#loading').css('visibility', 'hidden'); //and hide the rotating gif
                    }

                });

                $('ul li a').removeClass("current");
                $("'ul li a'[href='" + newHash + "']").addClass("current");
            });

        };

    });

    $(window).trigger('hashchange');

});

PHP:

<?php
if(empty($_POST['page'])) die("0");

$page = $_POST['page'];

//  validate it as alphanumeric before reading the filesystem
if (preg_match('/^[a-z0-9]+$/i', $_POST['page']) && file_exists('article/'.$page.'.html')) {
  echo file_get_contents('article/'.$page.'.html');
}
else echo 'There is no such page!';
?>

更新: firebug-plugin でこれを取得します:

エラー: 構文エラー、認識できない式: 'ul li a[href='anmeldung']

これが私の FireBug Console の外観です...これは URL が空だったということですか?

4

3 に答える 3

2

この行を変更します。

$("'ul li a'[href='" + newHash + "']").addClass("current");

これに:

$('ul li a[href="' + newHash + '"]').addClass('current');

少なくとも、それが FireBug が訴えている構文エラーです。

于 2013-08-22T18:57:54.980 に答える
0
 $(function() {

    var newHash      = "",
        $content = $("#content");



    $('ul li a').click(function() {
        window.location.hash = $(this).attr("href");
        return false;
    }); 

    $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);
        url=newHash
        if (newHash) {
            $content

                .fadeOut(200, function() {

         url=url.replace('#','');

 $('#loading').css('visibility','visible'); //show the rotating gif animation

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

               if(parseInt(msg)!=0) //if no errors
            {
                 $content.fadeIn(200, function() {
            $('#content').html(msg);}); //load the returned html into pageContet

            }  $('#loading').css('visibility','hidden');//and hide the rotating gif
        }

    });

                        $('ul li a').removeClass("current");
                        $('ul li a[href="' + newHash + '"]').addClass('current');
                    });

        };

    });

    $(window).trigger('hashchange');

});

ああ、エラーが見つかりました。問題は、URL を遅く設定したことです...これを見て解決を手伝ってくれたすべての人に感謝します ^^

于 2013-08-22T19:29:23.783 に答える