dpii73380 2013-07-24 14:05
浏览 59
已采纳

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

I'm working on a function to reload the page, but our menu which is actually an accordion should not be reloaded. Ie, to make it refresh the page to continue in the same state.

I'm showing below the functions I use JQuery to treat reload the page and consequently the same load in the center of the template.

This function below is the refresh of the page:

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);
        }
    }
});
}

The function below is the load view (page) in center of template:

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"));

    });
}
  • 写回答

1条回答 默认 最新

  • douhushen3241 2013-07-24 14:12
    关注

    This replaces the whole contents of #divConteudo with the ajax response:

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

    You'll have to target an inner div instead, and filter the response to get only the piece of content you want to update. Something like this:

    var targetDiv = $('#mioloDoConteudo');
    var newData = $(data).find('#mioloDoConteudo').html();
    targetDiv.html(newData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧