在容器中附加文本

时间:2016-10-13 12:46:22

标签: javascript html

我创建了一个容器,每次单击按钮时都会显示文本。我的代码工作正常,但每次我点击一个按钮时,容器中显示的上一个条目将被删除。我希望保留上一个条目,并在每次单击新按钮时使下一行显示下一行。下面是我的代码。谢谢你



function appChangeFunction() {    
  holdtext.innerText = appchange.innerText;
}

function sharedChangeFunction() {    
  holdtext.innerText = sharedchange.innerText;
}

<div id="container">
  <TEXTAREA ID="holdtext" rows="15" cols="70"></TEXTAREA><br><br>
</div>

<style>#container {width:100%; text-align:center;}</style>
<link rel="stylesheet" type="text/css" href="css.css" />

<button onclick="appChangeFunction()" style="background-color:white"    >App Change</button>&nbsp

<button onclick="sharedChangeFunction()" style="background-color:white" >Shared Change</button>&nbsp

<SPAN ID="appchange" STYLE="display:none"> Text Under App Change
  </SPAN>

<SPAN ID="sharedchange" STYLE="display:none"> Text Under Shared Change
</SPAN>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

只需使用+符号

将旧文本添加到新文本中即可
<script>
    function appChangeFunction() {    
        holdtext.innerText = holdtext.innerText + '\n' + appchange.innerText;
    }

    function sharedChangeFunction() {    
        holdtext.innerText = holdtext.innerText + '\n' + sharedchange.innerText;
    }
</script>

或使用+=符号和\n表示新行:

<script>
    function appChangeFunction() {    
        holdtext.innerText += '\n'+appchange.innerText;
    }

    function sharedChangeFunction() {    
        holdtext.innerText += '\n'+sharedchange.innerText;
    }
</script>

希望这有帮助。

答案 1 :(得分:1)

将脚本标记替换为:

<script>
function appChangeFunction() {    
holdtext.innerText += "\n"+appchange.innerText;
}

function sharedChangeFunction() {    
holdtext.innerText += "\n"+sharedchange.innerText;
}
</script>

答案 2 :(得分:0)

如果首先获取容器数据,则会添加文本

<html>
<body>
<div id="container">
<TEXTAREA ID="holdtext" rows="30" cols="70">
</TEXTAREA><br><br>
</div>

<style>#container {width:100%; text-align:center;}</style>
<link rel="stylesheet" type="text/css" href="css.css" />

<button onclick="appChangeFunction()"  style="background-color:white"    >App Change</button>&nbsp

 <button onclick="sharedChangeFunction()" style="background-color:white" >Shared Change</button>&nbsp

<SPAN ID="appchange" STYLE="display:none"> Text Under App Change
</span>

 <SPAN ID="sharedchange" STYLE="display:none"> Text Under Shared Change
</SPAN>

</body>
</html>
<script>
function appChangeFunction() { 
var x = document.getElementById("holdtext").innerHTML;   
holdtext.innerText = x + appchange.innerText;
}

function sharedChangeFunction() { 
var x = document.getElementById("holdtext").innerHTML;      
holdtext.innerText = x + sharedchange.innerText;
}
</script>