使用 Hash(#) 的 Ajaxpageloak,有数字时例外(例如:#1)

发布于 2024-12-11 03:25:04 字数 1867 浏览 0 评论 0原文

我正在使用这段代码:

<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 代码来运行。

如何在运行哈希中的代码时添加异常而不是添加异常?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-12-18 03:25:04

如果您想检查哈希字符串中是否存在数字,可以轻松使用 RegExp。
这样你就可以创建一个模式并检查一个字符串:

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

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.
望喜 2024-12-18 03:25:04

您可以通过添加检查来查看哈希是否为整数来添加异常。请参阅下面的代码。 (我只调整了 $('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;
    }
});

You can add an exception by adding a check to see if the hash is an integer or not. See code below. (I've only adjusted the $('a[rel=ajax]') section, the rest of the code is fine as is.)

//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;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文