使用 Javascript 捕获输入并提供输出
Capturing Input and Providing Output with Javascript
我终于开始学习了Javascript;我正在尝试做一些简单的事情:在提供输出的同时捕获键盘输入。我得到了键盘按下的半响应输出,但是一旦 enter
被按下,我就无法在页面的输出部分附加一行输入。我做错了什么?
<script language="javascript">
processCommand = function(cmd) {
if (this.text == null) this.text = "";
this.text = this.text + cmd + "<br>";
document.getElementById("outputID") = this.text;
}
document.onkeydown = function(evt) {
if (this.text == null) this.text = "";
var keyChar = String.fromCharCode(evt.keyCode);
this.text = this.text + keyChar;
if (evt.keyCode == 13) {
processCommand(this.text);
this.text = "";
}
document.getElementById("input").innerHTML = this.text;
};
</script>
<spad id="outputID"/><br>
<hr>
<spad id="input"/>
http://codepen.io/anon/pen/gavNOm
<script language="javascript">
processCommand = function(cmd) {
if (this.text == null) this.text = "";
this.text = this.text + cmd + "<br>";
outputID.innerHTML = this.text;
}
document.onkeydown = function(evt) {
if (this.text == null) this.text = "";
var keyChar = String.fromCharCode(evt.keyCode);
this.text = this.text + keyChar;
if (evt.keyCode == 13) {
processCommand(this.text);
this.text = "";
}
input.innerHTML = this.text;
};
</script>
<span id="outputID"/><br>
<hr>
<span id="input"/>
id 附加到 window 对象,顺便说一句,在函数中使用 vars 更容易混淆。
我终于开始学习了Javascript;我正在尝试做一些简单的事情:在提供输出的同时捕获键盘输入。我得到了键盘按下的半响应输出,但是一旦 enter
被按下,我就无法在页面的输出部分附加一行输入。我做错了什么?
<script language="javascript">
processCommand = function(cmd) {
if (this.text == null) this.text = "";
this.text = this.text + cmd + "<br>";
document.getElementById("outputID") = this.text;
}
document.onkeydown = function(evt) {
if (this.text == null) this.text = "";
var keyChar = String.fromCharCode(evt.keyCode);
this.text = this.text + keyChar;
if (evt.keyCode == 13) {
processCommand(this.text);
this.text = "";
}
document.getElementById("input").innerHTML = this.text;
};
</script>
<spad id="outputID"/><br>
<hr>
<spad id="input"/>
http://codepen.io/anon/pen/gavNOm
<script language="javascript">
processCommand = function(cmd) {
if (this.text == null) this.text = "";
this.text = this.text + cmd + "<br>";
outputID.innerHTML = this.text;
}
document.onkeydown = function(evt) {
if (this.text == null) this.text = "";
var keyChar = String.fromCharCode(evt.keyCode);
this.text = this.text + keyChar;
if (evt.keyCode == 13) {
processCommand(this.text);
this.text = "";
}
input.innerHTML = this.text;
};
</script>
<span id="outputID"/><br>
<hr>
<span id="input"/>
id 附加到 window 对象,顺便说一句,在函数中使用 vars 更容易混淆。