2011-10-19 11 views
0

使用していています:Ajaxpageloak(#)私はこのコードを数(例:#1)

<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 
     $('#ajax').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 
      $('#ajax').html(html); 

      //display the body with fadeIn transition 
      $('#ajax').fadeIn('slow');  
     }  
    }); 
} 
</script> 

だから、私が使用する必要があります:ページをAJAXを実行するために...しかし、私はいくつかの場所で使用しています:

たとえば、を参照してください...クリックすると、id = '1'にドライブするのではなく、実行するajaxコードを実行しています。

例外を追加するにはどうしたらいいですか?コード番号をハッシュで実行するときはどうしたらいいですか?

答えて

1

あなたのハッシュ文字列の数字の存在を確認したい場合は、簡単に正規表現を使用することができます。 あなたはパターンを作成し、文字列を確認することができますこの方法:はい、それは私が必要なものです

var pattern = new RegExp(/.*[0-9].*/); 
pattern.test(hash); // Will return TRUE if it contains any digit. 
+0

この作品.. :)を追加しました: \t var pattern = new RegExp(/.* [0-9]。* /); if(pattern.test(hash)!= true){} –

1

ハッシュが整数かどうかを確認するためのチェックを追加することで例外を追加できます。以下のコードを参照してください。 (私は$('a[rel=ajax]')セクションを調整してきているように、コードの残りの部分は結構です。)

//Search 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(/^.*#/, ''); 

    // test if hash is not an integer 
    if(parseInt(hash) != hash){ 

     //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 
     $('#ajax').hide(); 
     $('#loading').show(); 

     //run the ajax 
     getPage(); 

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

を...ハッシュは一つだけ番号が含まれているかいないかどうかをチェックし、私はあなたの例を使用して動作しませんでした... 私はJavaScriptの素人です。 –

+0

申し訳ありませんが、この例は機能しませんでした。元のコードで投稿したコードの1つのセクションのみを置き換えましたか? – whoisgregg

+0

はい、コードはすでに実行されていますが、if(parseInt(hash)!= hash)が機能しません:/ –

関連する問題