dongsha2792 2011-10-19 17:24
浏览 21
已采纳

带有哈希(#)的Ajaxpageloak,当有数字时例外(例如:#1)

I'm using this code:

<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>

So I have to use: page to run the ajax ... However, I am using in some places:

Go to some <a href='#1'> notices </ a> for example ... And when you click, instead of just driving to the id = '1 ', is doing the ajax code to run.

How do I add an exception and not when you run the code number in the hash?

  • 写回答

2条回答 默认 最新

  • dongpi3237 2011-10-19 18:04
    关注

    If you want to check the existence of a digit in your hash string, you can easily use RegExp. This way you can create a pattern and check a string :

    var pattern = new RegExp(/.*[0-9].*/);
    pattern.test(hash); // Will return TRUE if it contains any digit.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计