如何限制textarea并从textarea移动到另一个

时间:2014-09-12 13:41:48

标签: javascript css forms textarea wysiwyg

我正在建立'WYSIWYG',我有一点问题:

我有2个文字区域。

我想限制第一篇中的写作量,当我写完后,自动继续写第二篇文章。

就像在'Word'中一样:

http://postimg.org/image/5cuoxf6c9/

我正在使用execCommand所以我不希望它依赖于字体大小/空白等...

4 个答案:

答案 0 :(得分:0)

您可以在文本区域使用maxlength属性:

<textarea maxlength="number">

编辑:

你可以做这样的事情来包括输入字符

<textarea id="mytext" rows="4" cols="50" onkeyup="LimitCharacters(this);" >
</textarea>
<textarea id="mytext2" rows="4" cols="50">
</textarea>
<script>
function LimitCharacters(textArea) {
    var chars = textArea.value.length;
    if (chars > 10) {
        var words = textArea.value.split(" ");
        var lastWord = words.pop();
        textArea.value = words.join(" ");
        document.getElementById("mytext2").focus();
        document.getElementById("mytext2").value = lastWord;
    }
}
</script>

答案 1 :(得分:0)

会是这样的。 每次在textarea中输入时都要检查行数。 如果行数高于最大行数,请将焦点设置在另一个textarea上。

我还没有尝试过

String.prototype.lines = function() { return this.split(/\r*\n/); }
String.prototype.lineCount = function() { return this.lines().length; }

var maxLineCount = ...;

$('#textarea1').bind('input propertychange', function(e)
{
    if((this).val().lineCount > x)
    {
        e.preventDefault();
        $('#textarea2').focus();
    }
});

编辑:另一种方法是使用插件扩展textarea,同时在其中键入文本。并设置最大高度。达到最大高度后,将焦点放在另一个textarea

答案 2 :(得分:0)

试试这个:

<textarea id="firstTextArea" maxlength="100" />
<textarea id="secondTextArea" />

使用此脚本:

<script>
    $("#firstTextArea").on("keypress", function(){
        if($("#firstTextArea").length == 100){
            $("#secondTextArea").focus();
        }
    }
<script>

答案 3 :(得分:0)

I have tested it and working fine. 
below is script
<script>
function autotab(current,to){
    if (current.getAttribute && 
      current.value.length==current.getAttribute("maxlength")) {
        to.focus() 
        }
}
</script>

here is HTML ... please dont miss form tag with name attr.
<form name="note">
<textarea name="phone0" size=200 onKeyup="autotab(this, document.note.phone1)" maxlength=10></textarea>
<textarea name="phone1" size=200 onKeyup="autotab(this, document.note.phone2)" maxlength=10></textarea>
<textarea name="phone2" size=200 maxlength=10></textarea>
</form>