HTML--在textarea左侧添加行号

原文网址:HTML--在textarea左侧添加行号-CSDN博客

简介

本文介绍HTML在textarea文本框左侧添加行号的方法。

效果

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title>textarea测试</title>
    <style>
        .line-number-container {
            display: inline-block;
            padding: 2px 0 2px 1em;
            font-family: monospace;
            color: #999;
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: scroll;
            scrollbar-width: none; /* 禁止显示滚动条 */
        }

        .line-number-container::-webkit-scrollbar {
            width: 0; /* 禁止显示滚动条 */
        }

        .container textarea {
            width: 500px;
            height: 200px;
            padding-left: 2em;
            overflow: scroll;
        }
    </style>
</head>

<body>

<div class="container" style="position: relative;">
    <textarea wrap="off"></textarea>
    <div class="line-number-container"></div>
</div>

</body>

<script>
    createLineNumber('.container textarea', '.line-number-container')

    function createLineNumber(textAreaSelector, lineNumberSelector) {
        let textarea = document.querySelector(textAreaSelector);
        let lineNumber = document.querySelector(lineNumberSelector);
        updateLineNumbers();

        textarea.addEventListener("input", updateLineNumbers);
        textarea.addEventListener("scroll", syncScroll);
        lineNumber.addEventListener("scroll", syncContent);

        function updateLineNumbers() {
            let lines = textarea.value.split("\n");
            lineNumber.innerHTML = "";
            for (let i = 0; i < lines.length; i++) {
                let lineNum = document.createElement("div");
                lineNum.textContent = i + 1 + '';
                lineNumber.appendChild(lineNum);
            }

            let widthCount = String(lines.length).length;
            widthCount += 1;
            textarea.style.setProperty('padding-left', widthCount + 'em');
        }

        function syncScroll() {
            lineNumber.scrollTop = textarea.scrollTop;
        }

        function syncContent() {
            textarea.scrollTop = lineNumber.scrollTop;
        }
    }
</script>

</html>
相关推荐
zhippt_101411 小时前
教师做课件太耗时?AI生成PPT工具与图转PPT实测梳理
人工智能·html·powerpoint
winfredzhang13 小时前
从零手写一个毛玻璃风格的页面搭建台:单文件 HTML 实现组件放置、面板磁吸与图层管理
前端·html
沫璃染墨1 天前
从零入门计算机网络系列(十一):HTTP协议进阶——从重定向原理到GET/POST与Fiddler抓包
linux·服务器·开发语言·网络·c++·tcp/ip·html
前端架构师-老李1 天前
HTML与CSS基础第五课—HTML的空格、换行、横线相关知识
前端·css·html
echome8881 天前
CSS 布局:Flex 和 Grid 怎么选,附常见坑
前端·css·html
东鹏特饮1 天前
本机状态监控页面html
运维·css·html·css3
软件技术NINI2 天前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
产品经理爱开发2 天前
AxureShow(艾可秀)国内通用的html托管
前端·人工智能·html·ai编程
天若有情6732 天前
【前端趣味项目】把防抖玩出新花样!一个带隐藏后门的毫秒级“读秒挑战”
前端·javascript·html·防抖·外挂·趣味demo·读秒挑战
前端架构师-老李3 天前
HTML与CSS基础第四课—strong、em、b、i 各表示什么
前端·css·html