[英]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.