gpt4 book ai didi

javascript - 浏览器后退按钮,div 被 css 隐藏

转载 作者:行者123 更新时间:2023-12-03 02:12:43 24 4
gpt4 key购买 nike

我有一个网站,其中显示房地产搜索结果,例如使用 <div>默认情况下仅显示 6 个,并带有“加载更多”按钮。当用户单击“加载更多”按钮时,会显示另外 9 个帖子等。它是如何完成的:div默认情况下隐藏 ( display: none; ),然后 .show类已添加到接下来的 9 项中。

a("span.more-search-items").on("click", function(t) {
t.preventDefault();
var e = a(this).parents(".property-search");
e.find(".prop").not(".show").slice(0, 9).each(function() {
var t = a(this).attr("data-img");
a(this).css("background-image", "url(" + t + ")"), a(this).find(".main-image").attr("src", t), a(this).addClass("show")
}), 0 == e.find(".prop").not(".show").length && a("span.more-search-items").hide();
e.find(".prop.show").length

当用户转到单个属性然后点击返回浏览器按钮时,它会再次显示前 6 项。我尝试实现history.pushState()方法中,每次单击“加载更多”按钮时 URL 都会发生变化:

var href = window.location.href.replace(window.location.hash, '');
var currentPage = location.hash.replace("#","");
history.pushState(null, null, href + "#" + (+currentPage + 1));

URL 发生了变化,但情况仍然相同 - 当用户返回历史记录时,仅显示前 6 个结果。在这种情况下我可以做些什么吗?

最佳答案

如果您需要处理您需要的History API

  • 加载更多点击时,您需要推送或替换历史记录,例如带有哈希 #15 , #24 的可见 div 数量,如下所示

  • 对于推送/替换状态,您需要使用 $(window).on('popstate' , function(){alert(window.location.hash); }) 和 while 用户转到单个属性它会刷新所有页面,当单击浏览器后退按钮时,它会返回并刷新上一页..因此您需要检查 alert(window.location.hash); 页面加载时

最后,当您收到带有来自 window.location.hash 的编号的警报时,您可以根据它显示 div

好吧,让我用代码为您解释一下

$(document).ready(function(){

alert(window.location.hash); // check the alert here

$('.load_more_button').on('click' , function(){
if(window.location.hash){
var href = window.location.href.split('#');
var currentPage = parseInt(window.location.hash);
history.pushState(null, null, href[0] + "#" + (+currentPage + 1));
}
});
$(window).on('popstate' , function(){
alert(window.location.hash); // check the alert here
});
});

当你得到号码后,你可以从这里开始做你喜欢的事情

关于javascript - 浏览器后退按钮,div 被 css 隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49486048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com