jquery - jquery ajax() 后退按钮不会改变内容
问题描述
我用本地文件的 hashchange 制作了简单的 $.ajax(),但是后退按钮只更改 url,而不是内容。我发现没有什么似乎对我有用。有人可以帮我吗?
$(document).on('click', ".nextPage2", () => {
$.ajax({
url: 'page2.html',
dataType: "html",
success: function(result) {
$('.content').html(result);
}
});
window.location.hash = 'looks';
return false;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content"></div>
<button type="button" class="nextPage2" name="button">page2</button>
解决方案
当您更改 URL 时,您也在使用 JavaScript 来修改 DOM。
您需要监听URL 的更改并使用 JavaScript再次修改 DOM 。
function navigated() {
if (location.hash === '#something-different') {
$('.content').html("The content for something-different");
}
}
window.addEventListener("hashchange", navigated);
推荐阅读
- r - 如何根据日期值对特定列重新排序?
- javascript - 需要一些帮助来理解 javascript 变量范围
- maven - 在 Maven 中,我可以将 Zip 存档中的 JEE 项目打包到 SQL 脚本中吗?
- excel - 为什么 Excel 在环绕 AND 时不会失败?
- ios - UITableView Swift 中的断言失败
- javascript - 是否可以使用 node.js 获取已安装的 Windows 更新列表?
- c++ - std::ifstream 和读取文件
- css - 物化预构建主题不适用
- api - Api Token Authentication - 通过参数或标头?
- php - 如何修复未捕获的 ArgumentCountError:函数 Update::index() 的参数太少,