我有一个网站,其中有一个空框和一个输入文本框。我希望能够在输入框中输入内容并将其打印在空框中。
<div class='printchatbox'></div>
这是空盒子和
<input type='text' name='fname' class='chatinput'>
这是输入框。
.printchatbox
{border-width:thick 10px;border-style: solid;
background-color:#fff;
line-height: 2;color:#6E6A6B;font-size: 14pt;text-align:left;float: middle;
border: 3px solid #969293;width:40%;}
如果有人能告诉我怎么做,我会非常感激。感谢
答案 0 :(得分:27)
您使用onkeyup
事件
使用ID进行搜索要容易得多。将ID添加到元素中,如下所示:
<div class='printchatbox' id='printchatbox'></div>
<input type='text' name='fname' class='chatinput' id='chatinput'>
JS
var inputBox = document.getElementById('chatinput');
inputBox.onkeyup = function(){
document.getElementById('printchatbox').innerHTML = inputBox.value;
}
答案 1 :(得分:2)
<div class='printchatbox' id='printchatbox'></div>
<input type='text' name='fname' class='chatinput'
onkeyUp="document.getElementById('printchatbox').innerHTML = this.value" />
答案 2 :(得分:2)
有很多方法可以完成这项工作,最简单的方法是使用jQuery。在下面的示例中,我使用jQuery keyUp()
函数来侦听键盘事件,然后将更新后的值写入.printChatBox
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
</head>
<body>
<div class='printchatbox'>CHANGE ME</div>
<input type='text' name='fname' class='chatinput'>
<script type="script/javascript">
$('.chatinput').keyup(function(event) {
newText = event.target.value;
$('.printchatbox').text(newText);
});
</script>
</body>
</html>
我在这里发布了一个工作示例:http://jsbin.com/axibuw/1/edit
答案 3 :(得分:1)
在您的HTML中,
<div id='printchatbox'></div>
<br>
<input type='text' id='fname' class='chatinput' onkeyup="annotate()">
在JS中,
function annotate(){
var typed= document.getElementById("fname").value;
document.getElementById("printchatbox").innerHTML= typed;
}
答案 4 :(得分:-1)
Angular JS在两行代码中执行此操作:
导入其他库时,只需导入Angular JS:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js">
然后,在你的第一个div(你要复制的地方):
<input type="text" ng-model="myID"> </input>
然后,在您要显示内容的地方:只写:
<div> {{myID}}</div>
这是我见过的最好的解决方案!