Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX直接在单击时加载标签url_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 使用AJAX直接在单击时加载标签url

Javascript 使用AJAX直接在单击时加载标签url,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在用一个标签AJAX系统构建一个网站 每个页面都有一个标签。例如: <a href="#page1">Page 1</a> <a href="#page2">Page 2</a> 我如何在点击时采取行动?如果单击第1页或在URL中输入#page1,我希望直接显示第1页的内容 提前谢谢 通过将变量哈希传递给函数并将其用作数据检索器来解决此问题 $(document).ready(function () { if(window.loca

我正在用一个标签AJAX系统构建一个网站

每个页面都有一个标签。例如:

<a href="#page1">Page 1</a>
<a href="#page2">Page 2</a>
我如何在点击时采取行动?如果单击第1页或在URL中输入#page1,我希望直接显示第1页的内容


提前谢谢

通过将变量哈希传递给函数并将其用作数据检索器来解决此问题

$(document).ready(function () {

    if(window.location.hash == ''){

        document.getElementById('link_hem').click();

    }

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('active');

    //Seearch for link with REL set to ajax
    $('#menu a').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('active');
        $(this).addClass('active');

        //hide the content and show the progress bar
        $("#menu img#logo").attr("src", "ajax/loader.gif");

        //run the ajax
        getPage(hash);
    }); 
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) getPage();    
}

function getPage(hash) {

    //generate the parameter for the php script
    var data = 'page=%23' + hash;
    var page = window.location.hash.substring(1);

    $.ajax({
        url: "ajax/retriever.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //hide the progress bar
            $("#menu img#logo").attr("src", "images/menu_logo.png");    

            //add the content retrieved from ajax and put it in the #content div
            $('#page').html(html);

        }       
    });
}

它被称为文档片段标识符,而不是hashtag。
$(document).ready(function () {

    if(window.location.hash == ''){

        document.getElementById('link_hem').click();

    }

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('active');

    //Seearch for link with REL set to ajax
    $('#menu a').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('active');
        $(this).addClass('active');

        //hide the content and show the progress bar
        $("#menu img#logo").attr("src", "ajax/loader.gif");

        //run the ajax
        getPage(hash);
    }); 
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) getPage();    
}

function getPage(hash) {

    //generate the parameter for the php script
    var data = 'page=%23' + hash;
    var page = window.location.hash.substring(1);

    $.ajax({
        url: "ajax/retriever.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //hide the progress bar
            $("#menu img#logo").attr("src", "images/menu_logo.png");    

            //add the content retrieved from ajax and put it in the #content div
            $('#page').html(html);

        }       
    });
}