简体   繁体   English

使用JavaScript更改textarea的值

[英]Changing value of textarea with JavaScript

I'm trying to make a script that allows a user to enter text into a field, and then seeing that text entered into a textarea as if one was typing. 我正在尝试制作一个脚本,该脚本允许用户在字段中输入文本,然后看到该文本输入到textarea中,就像在键入文本一样。

JavaScript: JavaScript:

<script type="text/javascript">

//function to delay
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function changeValue() {
var text = document.getElementById("f").value;
var dst = document.getElementById("t").innerHTML;


var index;
for (index = 0; index < text.length; ++index) {
    dst += text.charAt(index);
    sleep(10);
}
}
</script>

HTML 的HTML

<textarea id="t"></textarea><br>
<input type="text" id="f">
<input type="button" onclick="changeValue();" value="Go">

However, clicking the button does nothing... What's the problem? 但是,单击该按钮没有任何作用...问题是什么?

Update 更新资料

Removing the sleep() function has made no change. 删除sleep()函数没有做任何更改。

You use innerHTML on TextArea. 您在TextArea上使用innerHTML。 You need to use .value 您需要使用.value

//function to delay
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}

function changeValue() {
    var text = document.getElementById("f").value;
    var dst = document.getElementById("t");


    var index;
    for (index = 0; index < text.length; ++index) {
        dst.value += text.charAt(index);
        console.log('a');
        sleep(1000);
    }
}

Instead of innerHTML you should use value on textarea: 您应该在textarea上使用value而不是innerHTML:

function changeValue() {
    var text = document.getElementById("f").value;
    var dst = document.getElementById("t"); // <-- reference textarea
    var index;
    for (index = 0; index < text.length; ++index) {
        (function(char) {
            setTimeout(function() {
                dst.value += char; // <-- imitate typing by appending chars
            }, 50 * index);
        })(text.charAt(index));
    }
}

Also consider using setTimeout for delay functionality, you current approach is not the way to go. 还可以考虑将setTimeout用于延迟功能,否则您当前的方法将不可行。

Demo: http://jsfiddle.net/SXV38/ 演示: http//jsfiddle.net/SXV38/

no need to define the function sleep, all you need to do is to call the changeValue function recursively with setTimeout just like this 无需定义函数sleep,您所需要做的就是使用setTimeout递归调用changeValue函数,就像这样

function changeValue(index) 
{
    if(typeof index=="undefined")
        index = 0;
    var text = document.getElementById("f").value;
    var dst = document.getElementById("t").innerHTML;
    setTimeout( function()
    {
        if(index<text.length)
        {
            document.getElementById("t").innerHTML += text[index];
            changeValue(index+1);
        }

    },200);
} 

you can see the example on jsFiddle 您可以在jsFiddle上看到示例

The sleep function locks the browser for the period it's "sleeping". 睡眠功能会在“睡眠”期间锁定浏览器。 If you want to simulate typing, use setTimeout to create a pause between actions that allows the browser to get on with other work: 如果要模拟输入,请使用setTimeout在动作之间创建一个暂停,以使浏览器可以继续进行其他工作:

<script>
function typeIt(msg, el) {
  var i = arguments[2] || 0;
  el.value = msg.substr(0, ++i);

  if (i < msg.length) {
    setTimeout(function(){typeIt(msg, el, i)}, 1000);
  }
}
</script>

<form>
  <input id="i0"><br>
  <input id="i1"><br>
  <input type="button"  value="Type it" onclick="
    typeIt(this.form.i0.value, this.form.i1)
  ">
</form>

There are a number of other approaches using closures and setInterval , however the above is simple and does the job (I think). 还有其他一些使用闭包和setInterval的方法 ,但是以上方法很简单并且可以完成工作(我认为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM