javascript - HTML/CSS/JS : How to make an illusion of a textarea with line numbers?

标签 javascript html textarea line-numbers

我想要一个在左侧显示行号的文本区域。换行应设置为“关闭”(以便水平滚动可用)。我想让我的页面成为一个独立的 .html 文件(不会有图形),所以我想避免任何第 3 方框架。

我应该走哪条路?你会怎么做?

最佳答案

我将从两个文本区域和同步机制开始。 像这样,

    <script>
    window.sync = function(e){
            var textarea = document.getElementById("lines");
            var source   = document.getElementById("some_text_area");
            textarea.scrollTop = source.scrollTop;
        }

        window.populate = function populate(){
            if(populate.started){
                return;
            }

            populate.started = true;
            var textarea = document.getElementById("lines");
            var str = '';
            for(var i=0;i < 100;i++){
                str = str + (i +'\r\n');
            }
            textarea.value = str;
        }
    </script>

<div>
<textarea 
    style="width:40px;overflow:hidden;height:40px;" 
    readonly="true" 
    id="lines"
></textarea>
<textarea 
    style="width:500px;height:40px;" 
    id="some_text_area" 
    onclick="populate()"
    onscroll="sync();"
></textarea>
</div> 

当然 populate() 函数(以及样式声明和事件声明)应该更健壮和智能,但是,它只是为了展示目的。

关于javascript - HTML/CSS/JS : How to make an illusion of a textarea with line numbers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907605/

相关文章:

javascript - 重新启动 Jquery setInterval 并重置所有变量

javascript - 在 MongoDB 中使用聚合框架按时间戳分组

javascript - Google Charts 加载多列图表时出现问题

javascript - jQuery - 在文本区域内插入一个字符串,然后选择它的一部分

html - 在文本框内的 css 中定位文本(背景图像)

php - 如何使用 jQuery/Javascript 更改日期格式?

html - 可以通过CSS做全屏透明渐变效果吗?

html - 在 Bootstrap 列之间添加边距,同时为 12 列宽的列删除边距

javascript - 在动态生成的输入上显示 jquery datepicker

html - 如何与其他不可调整大小的框一起使用可调整大小的文本区域?