简体   繁体   English

Safari - Javascript - 隐藏/显示Div会导致滚动条重置

[英]Safari - Javascript - Hiding/Showing Div causes scrollbar to be reset

I have a HTML document containing a series of Questions and Answers. 我有一个包含一系列问题和答案的HTML文档。

Each Question is contained in a div. 每个课题都包含在一个div中。 Each Answer is contained in a div. 每个答案都包含在div中。 Each Answer is given a unique id. 每个答案都有一个唯一的ID。

The CSS style for each Answer is set to 'none' in order to initially hide the element. 每个Answer的CSS样式设置为'none',以便最初隐藏元素。

When a Question is clicked on, the div passes the id of the corresponding Answer to this function: 单击问题时,div会将相应答案的ID传递给此函数:

function toggle(id) {
    var state = document.getElementById(id).style.display;
    if (state == 'block') {
        document.getElementById(id).style.display = 'none';
    } else {
        document.getElementById(id).style.display = 'block';
    }
}

(This toggles the appearance of the Answer div.) (这会切换答案div的外观。)

The problem: This mechanism works well in Safari if the scrollbar is at the default (top) position. 问题:如果滚动条处于默认(顶部)位置,此机制在Safari中运行良好。 Otherwise, this will cause the scrollbar to reset to the default position, effectively ruining the usefulness of this mechanism on long pages. 否则,这将导致滚动条重置为默认位置,从而有效地破坏了此机制在长页面上的有用性。

Q. Is there any way to prevent the scrollbar position from being reset? :有没有办法防止滚动条位置被重置?

Thanks. 谢谢。

You've said that you're using 你说过你正在使用

<a href='#'>...</a>

...for the question. ......对于这个问题。 That's the problem, that's a link telling the browser to go to the top of the page. 这就是问题,这是一个告诉浏览器转到页面顶部的链接。 You have a couple of options for correcting it: 您有几个选项可以纠正它:

  1. Hook the click event on the link (or its parent, etc.) and cancel the default action. 挂钩链接(或其父级等)上的click事件并取消默认操作。 How you do that depends on how you hook the event. 你如何做到这一点取决于你如何挂钩事件。 It sounds like you're already hooking the event, so this is probably the simplest solution. 听起来你已经挂了这个事件,所以这可能是最简单的解决方案。 If you're using an onclick= style hook, return false; 如果你使用的是onclick= style hook,则return false; out of your handler function. 超出你的处理程序功能。 If you're using addEventListener , use preventDefault on the event passed into it. 如果您正在使用addEventListener ,请对传入其中的event使用preventDefault (You will presumably already be aware that most versions of IE don't support addEventListener , but rather attachEvent ; how you prevent the default is also different. This sort of thing is why I use a library like jQuery , Prototype , YUI , Closure , or any of several others to smooth over browser differences.) (你可能已经意识到IE的大多数版本都不支持addEventListener ,而是支持attachEvent ;你如何防止默认也是不同的。这就是为什么我使用像jQueryPrototypeYUIClosure这样的库,或其他任何一个可以平滑浏览器差异。)

  2. Use the javascript: pseudo-protocol instead of # , eg: 使用javascript:伪协议代替# ,例如:

     <a href='javascript:;'>...</a> 

    The pseudo-protocol tells the browser to run the script code in the href element. 伪协议告诉浏览器在href元素中运行脚本代码。 In the above, the code is just a ; 在上面,代码只是一个; (statement terminator), which does nothing. (声明终结者),什么都不做。 You could, of course, have it trigger your toggle function: 当然,你可以触发你的toggle功能:

     <a href='javascript:toggle("foo");'>...</a> 
  3. Don't use an a at all. 根本不要使用a Whether it's appropriate to use one depends on whether it really is conceptually a link , which is totally your call. 是否适合使用它取决于它在概念上是否真的是一个链接 ,这完全是你的呼唤。

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

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