【问题标题】:Text cursor is moving to left after every character文本光标在每个字符后向左移动
【发布时间】:2015-09-21 07:51:23
【问题描述】:

这是与我编写的代码相关的错误。

我正在制作一个文本长度验证字段,想出了某种工作代码。但是,我在这里所做的事情是导致每次键入时文本光标都会向左移动,这意味着您键入的文本会向后或一团糟。

下面发生了什么可能导致这种情况? 我想可能是接头?

JSFiddle 和下面的 jQuery

$(function(){

    var charLimit = 10;

    $('.input').keypress(function(e){
        if (e.which > 0){
            var string = $(this).text().split("");
            for(i=0;i<charLimit;i++){
                if(string[i] !== undefined){
                    string.splice((charLimit-1),0,"<span class='error'>");
                    string.push("</span>");
                }
            }
            $(this).html(string.join(""));
        }
    });

});

【问题讨论】:

  • 我怀疑$.html 每次使用时都会重新初始化插入符号位置。
  • @JoshStevenson 非常好。祝你好运!
  • 请注意,您可以使用正则表达式缩短此代码,但它仍然会遇到奇怪的插入符号放置问题:jsfiddle.net/1xqbLns5/7
  • @JoshStevenson 我猜你想要这样的东西 (jsfiddle.net/nh9hbxdf) 我在里面用过@RoryMcCrossan 的regex
  • 感谢regex @RoryMcCrossan

标签: jquery


【解决方案1】:

使用此功能将光标始终放在末尾:

function placeCaretAtEnd(el) {
    el.focus();
    if (typeof window.getSelection != "undefined"
            && typeof document.createRange != "undefined") {
        var range = document.createRange();
        range.selectNodeContents(el);
        range.collapse(false);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (typeof document.body.createTextRange != "undefined") {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.collapse(false);
        textRange.select();
    }
}

以后使用

 $(this).html(string.join(""));

像这样:

 placeCaretAtEnd( document.getElementById(".input") );

您可能正在寻找这个:http://jsfiddle.net/nh9hbxdf/

这可以通过使用一些 css 技巧来实现:

这是更新后的代码:

 <p id="output"></p>
 <textarea></textarea>
<style>
  textarea, #output {
                width: 500px;
                height: 10em;
                padding: 0.2em;
                margin: 0.2em;
                box-sizing: border-box;
                font: 13px arial;
                border: 1px solid silver;
                white-space: pre-wrap;
            }

            textarea {
                position: relative;
                -webkit-text-fill-color: transparent;
                overflow: auto;
            }

            #output {
                position: absolute;
                pointer-events: none;
                z-index: 1;
            }

            #output span:first-of-type {
                color: red;
                /*background-color: blue;*/
            }
.error{
    color:red;
}

<script>

 $(document).ready(function(){
                $('textarea').on('input', function() {
                    $('#output').html(this.value.replace(/^(.{10})(.*)$/, '$1<span class="error" contenteditable>$2</span>'));
                });

                $('textarea').focus();
            });     

</script>

【讨论】:

  • 谢谢老兄。这是一个庞大的脚本,它比我的整个代码都大!这是实现它的唯一方法吗? :(我不想弄乱我的小功能
  • 哦,刚刚也注意到了。这不是 jQuery,是吗?这只是 JavaScript。我敢打赌,我们可以缩短加载时间。
  • placeCaretAtEnd(e.target); 会更好。
  • 查看您的代码后@JoshStevenson 我猜这对您不起作用,因为此功能将始终将光标放在末尾What about the situation if you want to edit text by clicking in middle of already written text in your p tag
  • 您介意向我们解释一下 JSFiddle 吗?并摆脱第一个位。您的答案比将光标移到末尾要好几英里
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 2017-06-02
  • 1970-01-01
相关资源
最近更新 更多