简体   繁体   English

DOM操作后如何防止页面的滚动位置重置?

[英]How to prevent page's scroll position reset after DOM manipulation?

I've got two JS functions, one that is adding options to a select box 我有两个JS函数,一个是为选择框添加选项

function addOption(selectId, text, value) {
    var selectbox = document.getElementById(selectId);
    var optNew = document.createElement('option');
    optNew.text = text;
    optNew.value = value;
    try {
        selectbox.add(optNew, null); //page position resets after this
    }
    catch(ex) {
        selectbox.add(optNew);
    }    
}

and another that is doing a document.getElementById(formId).appendChild(newHiddenInput) in a similarly simple function. 另一个在类似的简单函数中执行document.getElementById(formId).appendChild(newHiddenInput)。

They both work, elements are added as expected. 它们都工作,元素按预期添加。 However, upon calling either of them, the page resets its scroll position to the top of the page in both IE6 and FF. 但是,在调用它们中的任何一个时,页面将其滚动位置重置为IE6和FF中的页面顶部。 There is no postback, this is purely clientside manipulation. 没有回发,这纯粹是客户端操纵。 I've set breakpoints in Firebug, and it occurs immediately after the element.appendChild or select.add gets executed. 我在Firebug中设置了断点,它在element.appendChild或select.add执行后立即发生。 I know I can use JS to manually set a scroll position, but I didn't think it was necessary when the page isn't being re-rendered. 我知道我可以使用JS来手动设置滚动位置,但是当页面没有被重新渲染时我认为没有必要。

I'm no expert with JS or the DOM, so I may very well be missing something, but I've looked here and ran through their examples with the Try it Here options and I can't replicate the problem, indicating the codebase I'm working with is the culprit. 我不是JS或DOM的专家,所以我很可能会遗漏一些东西,但我看过这里并通过Try it Here选项运行他们的示例,我无法复制问题,表明代码库我与之合作是罪魁祸首。

Any ideas why the scroll position is being reset? 任何想法为什么滚动位置被重置? jQuery is available to me as well, if it provides a better alternative. 如果它提供了更好的替代方案,我也可以使用jQuery。

If the functions are being called from a link you might have an internal anchor in your link: 如果从链接调用函数,则链接中可能包含内部锚点:

http://www.website.com/page.html#

This is causing said behavior. 这导致了所述行为。 The default behavior is that if an anchor does not exist, the page scroll position jumps to the top ( scrollTop = 0 ). 默认行为是,如果锚不存在,页面滚动位置将跳转到顶部( scrollTop = 0 )。

If this happens on every function call regardless of the source, then this can be crossed off the list. 如果在每个函数调用中都发生这种情况而不管源是什么,那么这可以从列表中划掉。

What is activating the event? 什么是激活活动?

If it's an anchor then on the click event you need to "return false;" 如果它是一个锚点然后在点击事件上你需要“返回false”; after the call to your jQuery/Ajax/jScript code. 在调用jQuery / Ajax / jScript代码之后。

If it's a button you may need to do the same. 如果是按钮,您可能需要执行相同的操作。

I had this issue yesterday and this was the resolution. 我昨天遇到了这个问题,这是决议。

So <a href="#" onclick="DoStuff(); return false;">My link</a> 所以<a href="#" onclick="DoStuff(); return false;">My link</a>

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

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