简体   繁体   English

jQuery .toggle()和浏览器后退按钮

[英]Jquery .toggle() and the browser back button

I have a fairly simple layout with a very strange outcome. 我的布局很简单,结果却很奇怪。

$(".main").on("hover", ".js_post", function () {
$(this).children('.js_del').toggle();
});

Essentially, when a user hovers over the .js_post div, the .js_del div is toggled. 本质上,当用户将鼠标悬停在.js_post div上时,将切换.js_del div。 Here's the css that hides .js_del: 这是隐藏.js_del的CSS:

.js_del { display:none; }

Now, when the cursor hovers over .js_post, the .js_del div toggles, as expected. 现在,当光标悬停在.js_post上方时,.js_del div将按预期切换。 But, when the user clicks a link within the .js_post div and then clicks the browser back button, strange things happen... 但是,当用户单击.js_post div中的链接,然后单击浏览器后退按钮时,会发生奇怪的事情……

In FF, all works as one would expect (ie, the browser interprets the click as a mouseleave and toggles the .js_del.) 在FF中,所有工作均符合人们的预期(即,浏览器将单击解释为鼠标离开并切换.js_del。)

In Safari, however, when the user clicks back, the browser applies toggle in reverse (ie, .js_del is showing and when the cursor hovers over .js_post, the .js_del link disappears.) 但是,在Safari中,当用户单击时,浏览器将反向应用切换(即,显示.js_del,并且当光标悬停在.js_post上时,.js_del链接消失)。

Even weirder... 甚至更奇怪...

I decide to add a handler to manually hide the .js_del div whenever an 'a' element within .js_post is clicked like this: 我决定每次单击.js_post中的'a'元素时,都会添加一个处理程序以手动隐藏.js_del div:

$(".js_post").find("a").click(function () {
    $(this).parents('.js_post').children('.js_del').hide();
});

And now, when viewed in Safari, all works as expected but in FF, it's reversed (ie, .js_del is showing and when the cursor hovers over .js_post, the .js_del link disappears)! 现在,当在Safari中查看时,所有功能都可以按预期运行,但在FF中却相反(即显示.js_del,并且当光标悬停在.js_post上时,.js_del链接消失)!

Any thoughts??? 有什么想法吗??? Thanks! 谢谢!

Because the back button is not making the page cacheing behave the same, and you can't be sure of the dom element state, toggle() calls can be rough. 因为后退按钮不能使页面缓存的行为相同,并且您不能确定dom元素的状态,所以toggle()调用可能很粗糙。 I split out the mouseenter and mouseleave instead of hover() , and specifically called out the visibility within the child selector. 我将mouseenter和mouseleave分开,而不是hover() ,并专门在子选择器中调用可见性。 I also threw on a .hide() call at the very end to standardize the .js_del dom element visibility state regardless of back button browser issues. 最后,我也抛出了.hide()调用,以标准化.js_del dom元素的可见性状态,而不考虑后退按钮浏览器的问题。

js js

$(".main").on({
  "mouseenter":function(evt){
    $(this).children('.js_del:hidden').toggle();
  },
  "mouseleave":function(evt){
    $(this).children('.js_del:visible').toggle();
  }
}, ".js_post", null).find('.js_del:visible').hide();

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

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