表单提交,页面滚动到必填项位置

复制代码
<body>
    <div style="display:flex;flex-flow: column;">
        
        1<input value="" style="margin:200px; border:1px solid red"></input>
        2<input value="" style="margin:200px; border:1px solid red"></input>
        3<input value="" style="margin:200px; border:1px solid red"></input>
        4<input value="" style="margin:200px; border:1px solid red"></input>
        5<input value="" style="margin:200px; border:1px solid red"></input>
        6<input value="" style="margin:200px; border:1px solid red"></input>
    </div>

    <button onClick="test()">tijiao</button>
    <script>
        function test(){
            const inputs = Array.from(document.getElementsByTagName('input'));
            const emptyIndex = inputs.findIndex(input => input.value === '');
            inputs[emptyIndex].scrollIntoView({behavior: 'smooth'});

        }
    </script>
    </body>

还有使用锚点来实现的方式,比较方便

复制代码
<!DOCTYPE html>
<html>
<head>
    <style>
        /* 用于创建占位符,确保定位不被导航栏等固定元素遮挡 */
        #placeholder {
            display: block;
            height: 100px;
            margin-top: -100px;
            visibility: hidden;
        }
    </style>
</head>
<body>
    <nav>
        <ul>
            <li><a href="#section1">跳转到 Section 1</a></li>
            <li><a href="#section2">跳转到 Section 2</a></li>
            <li><a href="#section3">跳转到 Section 3</a></li>
        </ul>
    </nav>
    <div id="placeholder"></div>
    <section id="section1" style="margin:900px">
        <h2>Section 1</h2>
        <!-- 内容 -->
    </section>
    <section id="section2" style="margin:900px">
        <h2>Section 2</h2>
        <!-- 内容 -->
    </section>
    <section id="section3" style="margin:900px">
        <h2>Section 3</h2>
        <!-- 内容 -->
    </section>
</body>
</html>
相关推荐
慌糖1 分钟前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
Mintopia7 分钟前
Three.js 深度冲突:当像素在 Z 轴上玩起 "挤地铁" 游戏
前端·javascript·three.js
Penk是个码农12 分钟前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc
MrSkye15 分钟前
🔥JavaScript 入门必知:代码如何运行、变量提升与 let/const🔥
前端·javascript·面试
白瓷梅子汤19 分钟前
跟着官方示例学习 @tanStack-form --- Linked Fields
前端·react.js
爱学习的茄子23 分钟前
深入理解JavaScript闭包:从入门到精通的实战指南
前端·javascript·面试
zhanshuo1 小时前
不依赖框架,如何用 JS 实现一个完整的前端路由系统
前端·javascript·html
火柴盒zhang1 小时前
websheet在线电子表格(spreadsheet)在集团型企业财务报表中的应用
前端·html·报表·合并·spreadsheet·websheet·集团财务
讨厌吃蛋黄酥1 小时前
智能前端新纪元:语音交互技术与安全实践全解析
javascript
khalil1 小时前
基于 Vue3实现一款简历生成工具
前端·vue.js