dongshanyan0322 2012-02-02 22:13
浏览 50

返回按钮上的jQuery查找哈希标记和点击事件

I am working on my website that allows people to post content (similar to a twitter) and am trying to make it more streamline via jQuery/Ajax. There is many categories that people can choose to post in and each is organized by the main category, or section if you will.

Every time a user clicks on a section in the navigation panel to the left, that item moves up to the top of the list and expands downward displaying all of the categories in that section. Another thing that happens is the main content area fades out and in with the new content (the user posts) belonging to that section and categories. This has been going great so far and looks awesome!

However, say you click on a section and category to see all of the posts in that specific category. Then you click on a post to go inside of the post and see all the information, comments, etc.. Now, you want to go back to where you were so you hit your back button. When doing this right now it is going back and displaying the main page details again, not the category you had previously selected to look through.

I have added the following code to display which category is select (example: "/#/community") and to also check the hash then fire the "click" event to trigger that category to be shown once again.

last_known_hash = location.hash;
window.setInterval(function() {
    if(last_known_hash != location.hash) {
        $('#categories-panel a[name="'+ location.hash.substring(2) +'"]').click();
        last_known_hash = location.hash;
    }
}, 500);

Upon clicking back it does still show the "#/community" in the URL, however it does not fire the click. This is odd because if you type in the "#/community" into the URL it does work properly. So I am not really sure on why it's not being triggered properly on clicking "back". I'm not sure if this is the best/right way to go about doing this, but it's all I could think of at the moment. Maybe do a session instead? As always, any help would be appreciated :)

P.S. I know the click event works on it's own as when I run it in firebug it works perfectly.

SUMMARY: When someone clicks "back" in their browser, I need it to go to where they were last, Example: sitehere.com/#/community - and then fire a click event to display the "community" category content once again.

  • 写回答

2条回答 默认 最新

  • dongqiao2077 2012-02-02 22:22
    关注

    I think this plugin: https://github.com/cowboy/jquery-hashchange solves your problem

    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序