如何从文本框中获取元素并将其存储在数组中
How to fetch elements from a text box and store them in an array
谁能帮我把数据存储在从文本框中获取的数组中。我需要设计一种格式,以便在按下空格时,它应该在空格字符之前显示不同的字体颜色。空间之后的元素应该像往常一样。
编辑:
<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/
只需单击结果区域并开始键入。我只完成了空间功能,您需要添加其他内容,例如退格键......
相关文章:
- Meteor会话变量未存储数组
- 在Javascript的Textbox中存储数组中的值
- 如何保留输入复选框值的存储数组
- 如何在 localStorage 中存储数组
- 如何在打包的 chrome 应用中本地存储数组
- 如何在 html5 的本地存储对象中存储数组
- 需要循环访问 json 数据的本地存储数组中存储的 json 对象
- 在 Javascript 中创建存储数组,并在每次单击时推送元素 ID
- 根据选中的复选框存储数组元素
- 如何存储数组javascript
- 存储数组在Chrome扩展
- 什么是最有效的方式来存储数组从PHP到JS/jQuery,这样它是可重用的
- Javascript存储数组值
- 存储数组到Cookie Jquery
- Ruby在遍历函数时尝试存储数组元素
- 如何从mongodb的形式存储数组
- 如何在html中打印存储在本地存储数组中的值
- 使用Firebase存储数组
- 获取字符串格式的MySQL存储数组并转换为JSON对象的数组
- 在html attr中存储数组值的最佳实践