繁体   English   中英

单击按钮以更新 JavaScript 中的文本

[英]Clicking a button to update text in JavaScript

<div id="div1"></div>
<button id="addText" onclick="addText()">Add Text</button><br>

我试图让这个按钮调用这个 function 来更新文本字段。 我不希望它 go 到操作页面或任何东西。 显示方面甚至都不重要。 我只是想找出如何通过单击按钮在后台更新变量。

function addText(){
    // create a new div element 
    var newDiv = document.createElement("div"); 
    // and give it some content 
    var newContent = document.createTextNode("Hi there and greetings!"); 
    // add the text node to the newly created div
    newDiv.appendChild(newContent);  
    // add the newly created element and its content into the DOM 
    var currentDiv = document.getElementById("div1"); 
    document.body.insertBefore(newDiv, currentDiv); 
}

尝试这个

function addText(){
    document.getElementById("div1").innerHTML = "your change text";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM