简体   繁体   English

如何在输入文本中将此Javascript与jQuery一起使用?

[英]How can i use this Javascript with jQuery in a INPUT TEXT?

I would like to know how can i use/transform this function in order to get work into a INPUT TEXT... 我想知道如何使用/转换此功能,以便将工作转化为输入文本...

Actually works if target is a DIV, but i would like use it into a INPUT TEXT... 如果目标是DIV,则实际上有效,但我想将其用于输入文本中...

The code i found and i want to use is: 我找到并想要使用的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


<script src="js/jquery.js" type="text/javascript"></script>
<script language="JavaScript">


 $(document).ready(function (){
  textWrite("THIS'S A MESSAGE, I WOULD LIKE WRITE IT INTO A INPUT TYPE TEXT", '#write', 50);
 });

 function textList()
 {
   max = textList.arguments.length;
   for (i = 0; i < max; i++)
  this[i] = textList.arguments[i];
 }

 function textWrite(txt, selector, time)
 {
  $(selector).empty();
  var x = 0; pos = 0;
  var tl = new textList
    ( 
     txt
    );
  var l = tl[0].length;
  textInterval(selector, tl, l, x, pos, time);
 }

 function textInterval(selector, tl, l, x, pos, time)
  { 
   var interval =
   setInterval(function() {
   $(selector).html(tl[x].substring(0,pos));
   if(pos++ == l) 
    clearInterval(interval);
   }, time);
  }

</script>


<title>Untitled Document</title>
</head>


<body>
<div id="write" /></div>
<input type="text" name="WriteHerePlease" id="WriteHerePlease" />
</body>
</html> 

Thank in advance for all the help you can give me. 预先感谢您能给我的所有帮助。

So in case of input field you just need to set value ( val method) instead of innerHTML ( html method): 因此,在输入字段的情况下,您只需要设置valueval方法)而不是innerHTMLhtml方法)即可:

function textWrite(txt, selector, time) {
    $(selector).val('');
    var x = 0;
    var pos = 0;
    var tl = new textList(txt);
    var l = tl[0].length;
    textInterval(selector, tl, l, x, pos, time);
}

function textInterval(selector, tl, l, x, pos, time) {
    var interval = setInterval(function () {
        $(selector).val(tl[x].substring(0, pos));
        if (pos++ == l) clearInterval(interval);
    }, time);
}

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

暂无
暂无

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

相关问题 如何使用JavaScript或Jquery动态改变HTML文本输入 - How to use JavaScript or Jquery to dynamically changes HTML text input 我怎样才能让 onchange 事件对 javascript/Jquery 的文本输入框更新起作用? - How can I make the onchange event work for the text input box update by the javascript/Jquery? 我如何在文本输入字段中输入数字而在javascript或jquery中没有模式 - how i can enter digit in text input field without pattern in javascript or jquery 我想使用无线电输入的文本。 我如何使用 jQuery 或 JavaScript 获得它 - I want to use the text of radio inputs. How can i get it using jQuery or JavaScript 我可以在jQuery .text()上使用Javascript字符串对象方法吗 - Can I use Javascript string object methods on jQuery .text() 当有人使用javascript或jquery粘贴文本时,如何仅在输入字段中的文本开头禁用/删除空格? - How can i disable/remove white spaces only at the beginning of text inside an input field when someone paste text using javascript or jquery? 如何在JavaScript中获取输入索引-可以使用jQuery - How to get index of input in javascript - can use jQuery 如何使用javascript / jquery防止在HTML文本字段中键入小数点? - How can I use javascript/jquery to prevent a decimal point from being typed into an HTML text field? 当按下下一个和上一个按钮时,如何使用javascript和jquery更改文本值? - How can I use javascript and jquery to change a text value when next and previous buttons are pressed? 如何在 JQuery 中控制多个输入文本 - How can I control multiple input-text in form in JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM