简体   繁体   English

后退按钮上的jQuery查找哈希标签并触发点击事件

[英]jQuery on back button find hash tag and fire click event

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. 我正在我的网站上工作,该网站允许人们发布内容(类似于Twitter),并试图通过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. 我添加了以下代码,以显示选择了哪个类别(例如:“ /#/ community”),并检查哈希,然后触发“ click”事件,以触发该类别再次显示。

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. 单击后,它仍会在URL中显示“#/ community”,但是不会触发点击。 This is odd because if you type in the "#/community" into the URL it does work properly. 这很奇怪,因为如果您在URL中键入“#/ community”,它将可以正常工作。 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 :) 一如既往,任何帮助将不胜感激:)

PS I know the click event works on it's own as when I run it in firebug it works perfectly. PS:我知道click事件可以独立运行,因为当我在firebug中运行它时,它可以完美运行。

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. 简介:当有人在浏览器中单击“后退”时,我需要它转到他们的最后位置,例如:sitehere.com/#/community-然后触发click事件以再次显示“社区”类别的内容。

historyJS and HTML5 history popState and pushState. historyJS和HTML5历史记录popState和pushState。 Those are the terms you need to learn to do what you're trying to do. 这些是您要做的事情需要学习的术语。

我认为这个插件: https : //github.com/cowboy/jquery-hashchange解决了您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM