繁体   English   中英

不要重新充电菜单以使用Jquery-Ajax刷新页面

[英]Don't recharge the menu to refresh the page with Jquery-Ajax

我正在使用一个功能来重新加载页面,但是实际上不应重新加载我们实际上是手风琴的菜单。 即,使其刷新页面以继续在相同状态下进行。

我在下面显示使用JQuery处理的功能,请重新加载页面,并因此在模板的中心进行相同的加载。

下面的功能是页面的刷新:

function recarregarPagina() {

//Verifica quais teclas estão sendo pressionadas
$(document).on("keydown", function(e) {

    //Houve um evento para recarregar a página?
    if ((e.which || e.keyCode) === 116 || (e.keyCode === 82 && e.ctrlKey)) {
        e.preventDefault();

        //Inicializa a hash
        var hash = window.location.hash;
        console.log("recarregarPagina 1=" + hash);

        //A última url está definida?
        if (hash !== '' && hash !== undefined) {
            console.log("recarregarPagina 2=" + hash);

            //Recarrega a página central do template de acordo com a última url visitada
            loadView(hash, false, true, true);
        }
    }
});
}

以下功能是模板中心的加载视图(页面):

function loadView(url, post, submit, repeat) {

//Remove o hash da url para poder carregá-la sem a ocorrência de loop
url = url.replace("#", "");

//Inicializa variáveis
var div = $("#divConteudo");
var overlay = $("#overlay");


var type = "POST";
var data = $("form").serialize();

//Verifica se o tipo de requisição ajax será post ou GET
if (!post) {
    type = "GET";
}

//A última hash é diferente da url atual?
if (lastHash !== url || repeat) {

    //Exibe o loader
    overlay.show();
    div.hide();


    /**
     * Inicia a requisição AJAX
     */
    $.ajax({
        //Antes de enviar a requisição
        beforeSend: function() {
            //Atualiza o último hash visitado
            lastHash = url;

            //Insere a hash na url do navegador
            window.location.hash = url;

        },
        type: type,
        url: url,
        //Para enviar os dados junto com a página, verifica se será necessário o envio ou não,
        //pois dependendo do caso o $_POST pode atrapalhar o carregamento
        data: function() {

            //Não foi definido o envio?
            if (!submit) {
                //Não retorna os dados
                return null;

                //O envio foi definido, ou omitido
            } else {
                //Retorna os dados do formulário
                return data;
            }
        }


        //Quando a requisição estiver concluída
    }).done(function(data) {

        //Aguarda o carregamento da View
        $(div).html(data).load(function() {
        });

        //Remove o loader
        overlay.hide();
        div.show();

        //Inicializa o envento para tratar a submissão do form
        formSubmit($("form"));

    });
}

这将用ajax响应替换#divConteudo的全部内容:

$(div).html(data); // you don't need the .load you have after this

您必须改为定位一个内部div,然后过滤响应以仅获取要更新的内容。 像这样:

var targetDiv = $('#mioloDoConteudo');
var newData = $(data).find('#mioloDoConteudo').html();
targetDiv.html(newData);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM