简体   繁体   English

如何锚定JavaScript?

[英]How to anchor with javascript?

I am developing a web system in java, jsp. 我正在用Java,JSP开发Web系统。

I have at the end of my home page a contact form where I need to validate ... 我在主页的末尾有一个联系表单 ,需要在其中进行验证...

I made some (Ex: name can not be null) in javascript ... but I would also like to do the backend (servlet). 我在javascript中做了一些(例如:name不能为null)……但是我也想做后端(servlet)。

I'm using a button submit and sending the information to the servlet ... 我正在使用按钮提交并将信息发送到servlet ...

Assuming that in servlet validation the name is null, I feed a list of errors and show it just below the contact form. 假设在Servlet验证中名称为null,我提供了一个错误列表,并将其显示在联系表下方。

<c:if test="${not empty requestScope.erros}">

    <div class="erro">
        <c:forEach var="erro" items="${requestScope.erros}">
            <li><c:out value="${erro}"/></li>
        </c:forEach>
    </div>

</c:if>

The problem is that the form is at the bottom of the page, so for the user to know that there was an error he would have to scroll to the end to check. 问题是该表单位于页面底部,因此用户必须滚动到末尾才能检查该错误,以使用户知道该错误。

Is there any way to make a kind of "anchor javascript"?When page loads if errors! 有什么方法可以制作“锚javascript”?页面加载时如果出错! = Null scroll page to contact div. =空滚动页面以联系div。

Yes there is, 就在这里,

if there are errors you can insert this script : 如果有错误,您可以插入以下脚本:

<script>
   var elmnt = document.getElementById("idOfErrorDiv");
   elmnt.scrollIntoView();
</script>

It looked like this: 它看起来像这样:

$(document).ready(function () {

                    var erros = document.getElementById("erro");
                    if (erros != null) {
                        $([document.documentElement, document.body]).animate({
                            scrollTop: $("#contato").offset().top
                        }, 2000);
                    }


                })

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

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