Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中模拟键入的外观,而不是实际的按键_Javascript_Jquery_Dom - Fatal编程技术网

在javascript中模拟键入的外观,而不是实际的按键

在javascript中模拟键入的外观,而不是实际的按键,javascript,jquery,dom,Javascript,Jquery,Dom,我正在尝试编写一个简单的函数,使它看起来像是有人在textarea --这是我的函数(请原谅,如果它很糟糕,但我通常不使用javascript)--- console.log()部分工作正常,但由于某些原因,我无法让这个脚本以我期望的方式更新dom function type(string) { value = ""; el = document.getElementById("typeArea"); fo

我正在尝试编写一个简单的函数,使它看起来像是有人在
textarea

--这是我的函数(请原谅,如果它很糟糕,但我通常不使用javascript)---
console.log()
部分工作正常,但由于某些原因,我无法让这个脚本以我期望的方式更新dom

        function type(string) {
            value = "";

            el = document.getElementById("typeArea");
            for (var i = 0; i < string.length; i++) {
                value += string[i];
                //$("#fbw > textarea").val(value);
                el.textContent = value;
                console.log(value);
                sleep(160);
            }
            sleep(2000);
        }
函数类型(字符串){
value=“”;
el=document.getElementById(“typeArea”);
对于(变量i=0;itextarea”).val(值);
el.textContent=值;
console.log(值);
睡眠(160);
}
睡眠(2000年);
}

我很感激你能给我的任何见解

您可以使用setTimeout函数执行类似操作。

您所缺少的只是一个构造,而不是
Sleep
。实现这一点的js方法是使用超时和递归调用来迭代字符串

function type(string,element){
 (function writer(i){
  if(string.length <= i++){
    element.value = string;
    return;
  }
  element.value = string.substring(0,i);
  if( element.value[element.value.length-1] != " " )element.focus();
  var rand = Math.floor(Math.random() * (100)) + 140;
  setTimeout(function(){writer(i);},rand);
 })(0)
}
函数类型(字符串、元素){
(函数编写器(i){

如果(string.length如果不使用jQuery,为什么要标记这个jQuery?会发生什么?另外,jsfiddle/etc对于这样的东西很好。jQuery是我愿意使用的一个选项。事实上,我先尝试了jQuery。好的一点,我会立即添加一个jsfiddle。除了循环中应该更新con的部分之外,所有东西都可以工作textarea的帐篷…甚至console.log都会显示每个更新…我会立即尝试,但你知道我已经编写的代码为什么不起作用吗?嗯,你没有任何提琴或代码笔,所以很难说。啊,这正是我想要的,非常优雅-!谢谢你,它工作得很有魅力,这也很好地解释了我为什么要这么做代码不起作用。此代码的唯一缺点是,在IE中,它会获得它所写的每个字母的焦点,因此页面变得无用。正在寻找一个解决方案,但当时对IE无效。为什么对
getElementsByClassName
不起作用?在何种意义上,您试图将getElementsByClassName用于此方法?
function type(string,element){
 (function writer(i){
  if(string.length <= i++){
    element.value = string;
    return;
  }
  element.value = string.substring(0,i);
  if( element.value[element.value.length-1] != " " )element.focus();
  var rand = Math.floor(Math.random() * (100)) + 140;
  setTimeout(function(){writer(i);},rand);
 })(0)
}