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

复制代码
<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>
相关推荐
其美杰布-富贵-李13 分钟前
第 6 篇:相机与 OrbitControls
前端·javascript·three.js
lichenyang45317 分钟前
从图片生成任务到用户隔离:AIGC Creative Studio 后端与 PostgreSQL 建模实践
前端·后端
小黑技术栈1 小时前
关于前端的一些话
前端
无人生还1 小时前
从 Vue3 到 React · 快速上手系列第 11 篇:状态管理
前端·vue.js·react.js
GuWenyue1 小时前
90%前端写React TS都踩坑!一套父子组件+Hooks完整实战,彻底搞懂类型约束
前端·react.js
Hilaku1 小时前
当 AI 一天写完一周的代码,我们还剩什么优势?
前端·javascript·程序员
无责任此方_修行中2 小时前
换个版本号就能升级?可没那简单:pnpm 11 升级踩坑记
javascript·后端·npm
尘世中一位迷途小书童2 小时前
无人机航线里的云台角度怎么调?Cesium 做了个视锥 + 鹰眼联动
前端·javascript·数据可视化
张元清2 小时前
React useMeasure Hook:用 ResizeObserver 测量 DOM 元素 (2026)
javascript·react.js
思码梁田2 小时前
CSS display 属性:从元素类型转换到隐藏元素的实用指南
前端·css·display·inline·block·none·inline-block