我有一段代码,当我在文本字段中输入文本时显示div。它工作正常。现在我想通过将按钮文本克隆到文本字段来输入文本。文本显示在文本字段中,但div没有显示出来。
function addText(event) {
var targ = event.target || event.srcElement;
document.getElementById("user_notation").value += targ.textContent || targ.innerText;
}

<input id="user_notation">
<button onclick="addText(event)">C</button>
&#13;
答案 0 :(得分:0)
您可以调用show函数,该函数在.addText()
的内容中显示div中的值:
function addText(event) {
var targ = event.target || event.srcElement;
document.getElementById("user_notation").value += targ.textContent || targ.innerText;
showText(); //Show value in div
}
function addText(event) {
var targ = event.target || event.srcElement;
document.getElementById("user_notation").value += targ.textContent || targ.innerText;
showText();
}
document.getElementById("user_notation").addEventListener('input', showText, false);
function showText(){
document.getElementById("result").innerHTML = document.getElementById("user_notation").value;
}
&#13;
<input id="user_notation">
<button onclick="addText(event)">C</button>
<div id="result"></div>
&#13;
答案 1 :(得分:0)
在选项
下方实现预期用途HTML:
<input id="user_notation" onkeyup ="showDiv()">
<button class="div" onclick="addText(event)" >C</button>
<div id="div">showing DIV</div>
JS:
document.getElementById("div").style.visibility = 'hidden';
function addText(event) {
var targ = event.target || event.srcElement;
document.getElementById("user_notation").value += targ.textContent || targ.innerText;
}
function showDiv(){
var textboxVal = document.getElementById("user_notation").value;
if(textboxVal.length>0){
document.getElementById("div").style.visibility = 'visible';
}
else{
document.getElementById("div").style.visibility = 'hidden';
}
}