【问题标题】:window.scrollTo not working when used in iframe在 iframe 中使用时 window.scrollTo 不起作用
【发布时间】:2017-08-30 07:16:40
【问题描述】:

我的页面上有一个 继续 按钮,当用户单击该按钮时,我会调用验证函数,如果页面上有任何错误,我会将它们显示在顶部并使用 window.scrollTo(0,0); 滚动到它们

现在,此页面已加载到 iframe 中,而 window.scrollTo(0,0); 不起作用。我尝试了所有可能的方法,例如window.parent.scrollTo(0,0);$('#error_msg_smry').animate({ scrollTop: 0 }, 0);,但没有任何效果。

下面是我的代码(下面是错误信息 div):

<div id="error_msg_smry" class="error_msg_summary" style="display:none;" >
    <br/>
    <div class="error_msg_heading">Sorry, but we cannot process your donation until the following errors have been fixed:</div>
    <div>&nbsp;</div>
    <div class="error_messages">
        <div id="selDonationErr" class="textClass" style="color:#b94a48; font-weight:400;"></div> 
        <div id="otherAmtErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="lessDonationErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="recipientErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="firstNameErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="lastNameErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="emailErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="creditCardNameErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="creditCardNumberErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="creditCardExpiryMonthErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="creditCardExpiryYearErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
        <div id="creditCardCCVErr" class="textClass" style="color:#b94a48; font-weight:400;"></div>
    </div>
    <br/>
</div>

下面的按钮:

<apex:commandButton styleclass="btn" action="{!makeDonation}" onClick="return validate();" value="Continue"/>    

在我的验证函数中,我执行以下操作:

if(isPass == false){
    document.getElementById("error_msg_smry").style.display = 'block';
    window.scrollTo(0,0);
}

我无权访问父页面(在我的 iframe 之外)。
谁能帮帮我

【问题讨论】:

  • 请问有什么更新吗?

标签: javascript jquery html css iframe


【解决方案1】:

如果有人遇到同样的问题并寻找解决方案,请发布我的答案:

我用下面的代码解决了它:

  if(isPass == false){
                    document.getElementById("error_msg_smry").style.display = 'block';
                    // window.scrollTo(0,0);

                    var div = document.createElement("div");
                      div.innerHTML = "<a style='position:absolute;left:0;top:0px' href='#'></a>";
                      div = div.firstChild;
                      document.body.appendChild(div);
                      div.focus();
                      div.parentNode.removeChild(div);
                 }
                else{
                   document.getElementById("error_msg_smry").style.display = 'none';
                }

                return isPass;
            }

【讨论】:

    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2021-04-06
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多