【发布时间】:2019-06-10 13:35:10
【问题描述】:
我正在尝试伪造页面 url,同时使用 ajax 来更改页面内容。我还在结合使用 jquery 转换和这个 ajax,但是当我添加 pushstate 来更改 URL 时,它会阻止 ajax 更改页面内容。
如果我注释掉 // window.history.pushState(null, "null", href) 行,那么转换工作正常,但 url 不会改变。谁能看到我做错了什么?
$("#contactRoll").on("点击", function(event) {
event.preventDefault();
$(document).attr("title", "Contact Page");
//get the 'fake' link
const href = $(this).attr("href")
//fake the url
window.history.pushState(null, "null", href)
$('.main-logo').fadeOut(500)
$('.main-logo-reverse').delay(500).fadeIn(300)
$.ajax({
//set the fake url
url: href,
success: function (data) {
$("header").animate({marginTop: "100vh"}, function () {
const newPage = $(data).filter("#main").html()
$("#main").html(newPage)
$("section#contact").animate({marginTop: "0vh"})
})
}
})
})
【问题讨论】:
标签: jquery ajax browser-history pushstate