c# - 带有回车键的文本框行号

标签 c# jquery html ajax

我正在尝试实现一个文本插入字段,它应该自动为每一行编号(基于换行符)。

用户不应该自己选择这个或删除数字,文本字段应该从一开始就配置为一个编号列表。

这是在 JQuery 中完成的,我还没有找到解决方案。

以下是我尝试过的方法,但它无法正常工作。 我从 LineAreaText Demo 得到了解决方案.

还有JSFIDDLE

但需要像 jsfiddle 文本框这样的结构:


1. here is first line.
2. when press enter key this two will add in this line and 
   now I am not pressing enter key so there is no next line 
   count.
3. Now pressed enter and third is there as line number.

我试过了

  • 如上所述使用可编辑的 div 结构但无法放置空白 当没有按下回车键且句子尚未完成时。
  • 使用 ajax htmleditor extender,但需要按顶部的有序列表图标按钮。

这是 JsFiddle 使用的与我需要做的相同。

enter image description here

我非常感谢这方面的任何帮助或指导,因为我现在被困住了。

提前致谢。

最佳答案

几行 JavaScript 就可以解决问题。 http://jsfiddle.net/mkWhy/1/您只需处理文本区域的 onkeyup 事件,将当前文本拆分为多行(在新行字符 '\n' 处拆分),遍历这些行并确保每行的开头都有正确的数字。

<textarea id="num" rows="5" cols="32">1. </textarea>

纯 JS

document.getElementById("num").onkeyup = function(e) {
    var evt = e ? e : event;
    if((evt.keyCode && evt.keyCode != 13) || evt.which != 13) 
        return;
    var elm = evt.target ? evt.target : evt.srcElement;
    var lines = elm.value.split("\n");
    for(var i=0; i<lines.length; i++)
        lines[i] = lines[i].replace(/(\d+\.\s|^)/, (i+1) + ". ");
    elm.value = lines.join("\n");
}

jQuery

$("#num").keyup(function(event) {
    if(event.which != 13)
        return;
    var elm = $(this);
    var lines = elm.val().split("\n");
    for(var i=0; i<lines.length; i++)
        lines[i] = lines[i].replace(/(\d+\.\s|^)/, (i+1) + ". ");
    elm.val(lines.join("\n"));
});

编辑 这更符合 OP 的问题,一个 jsfiddle 类型的编号文本输入。 http://jsfiddle.net/qZqX8/

我使用两个文本区域,第一个设置为只读,并让它们彼此相邻。然后在输入文本区域上使用 keyup 和滚动事件。保持高度和滚动位置同步。

$(".numbered").scroll(function(event) {
    $(this).prev().height($(this).height());
    $(this).prev()[0].scrollTop = this.scrollTop;
});
$(".numbered").keyup(function(event) {
    var elm = $(this);
    var lines = elm.val().split("\n");
    var numbers = "";
    for(var i=0; i<lines.length; i++)
        numbers += (i+1) + "\n";
    elm.prev().val(numbers);
    elm.prev()[0].scrollTop = this.scrollTop;
});

EDIT 2 这是一个类似于 JSFiddle.net 的编辑器的版本。我不处理文本突出显示、shift 或箭头键,但输入和退格键工作。 http://jsfiddle.net/gqHgb/

HTML

<div id="ref_line" style="display:none">
    <div class="line"><div class="lineno"></div><pre contenteditable="true"> </pre></div>
</div>
<div class="editor">
</div>

CSS 我正在使用 CSS counter() 来处理行号

.editor {
    margin-left: 2em;
    counter-reset: lnno;
}
.editor .line {
    poisition: relative;
}
.line .lineno {
    position: absolute;
    left: 0px;
    width: 2em;
    color: blue;
    text-align: right;
}
.line .lineno:before {
    counter-increment: lnno;
    content: counter(lnno);
}
.line pre {
    position: relative;
    overflow: visible;
    white-space: pre-wrap;
    word-break: normal;
    word-wrap: break-word;
}

JS jQuery

// setup editors
$(".editor").each(function() {
    $(this).append($("#ref_line").html());
});
// line focus / blur
$(".editor").on("focus", ".line pre", function() {
    var pre = $(this);
    if(pre.text() == " ")
        pre.text("");
});
$(".editor").on("blur", ".line pre", function() {
    var pre = $(this);
    if(pre.text() == "")
        pre.text(" ");
});
// line add / remove
$(".editor").on("keydown", ".line pre", function(event) {
    var pre = $(this);
    if(event.which == 13) {
        event.stopPropagation();
        event.preventDefault();
        pre.parent().after($("#ref_line").html());
        pre.blur();
        pre.parent().next().find("pre").focus();
    } else if(event.which == 8 && pre.text() == "" && this != pre.parents(".editor").find("pre:first")[0]) {
        var back = pre.parent().prev();
        pre.parent().remove();
        back.find("pre").focus();
    }
});

关于c# - 带有回车键的文本框行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751837/

相关文章:

c# - 新打开的窗口打开但立即关闭

c# - variable.ToString() 与 Convert.ToString(variable)

jquery - 未提交在搜索中不可见的数据表记录

PHP计算txt文件中的单词

html - 防止文本与之前的 div 重叠

PHP 在 HTML 表中显示 MySQL 结果

c# - 在 C# 中存储引用

C# - 如何发布列表

Jquery 图像转换没有按预期工作?

jquery - 如何通过Ajax将变量从grail的 Controller 传递到gsp文件?