如何从文本框中获取元素并将其存储在数组中

How to fetch elements from a text box and store them in an array

本文关键字:存储 数组 获取 文本 元素      更新时间:2023-09-26

谁能帮我把数据存储在从文本框中获取的数组中。我需要设计一种格式,以便在按下空格时,它应该在空格字符之前显示不同的字体颜色。空间之后的元素应该像往常一样。

编辑:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
  var x = 0
  function abc() {
      var a = new Array("a", "b", "c", "d", "e", "f", "g", ..... "h", "x", "y", "z")
      document.getElementById("type").value = a[x]
      var i = document.getElementById("type").value
      var n = i.charAt(i.length - 1)
      alert(n)
      var n = i.substring(0, i.length - 1)
      document.getElementById("type").value = n.concat(a[x]);
      if (x >= 0) x++
      if (x >= 26) x = 0
  }
</script>
</head>
<body>
<!--<input type="text" name="type" id="type" size="100" onkeyup="abc()">-->
<input type="text" name="type" id="type" size="100" onkeypress="abc()">
<div id="log"></div>

另一个尝试:

<script>
  e = jQuery.Event("keypress", {
      keyCode: 32
  });
  jQuery('#type').bind('keydown', function (e) {
      var s = e.which;
      jQuery('#log').html(e.which);
      if (s == 8) { //backspace
          alert('back space');
      }
      if (s == 32) { //space bar
          alert('space');
          var z = document.getElementById("type").value
          jQuery('#type').css({
              'color': 'blue'
          });
      }
  });
</script>
</body>
</html>

由于输入中不能有多种颜色,因此不应有输入,而只有一个标签,并且在用户键入时捕获事件并附加到该标签。

$("body").keypress(function (e) {
    var s = e.which;
    var c = String.fromCharCode(e.which);
    $("#log").children(":last").append(document.createTextNode(c));
    if (s == 32)  { // Space
        $("#log").children(":last").css({ 'color': 'blue' });
        $("#log").append($("<span/>"));
    }
});

下面是示例:http://jsfiddle.net/B5wwP/

只需单击结果区域并开始键入。我只完成了空间功能,您需要添加其他内容,例如退格键......