javascript - 单击按钮以更新 JavaScript 中的文本
问题描述
<div id="div1"></div>
<button id="addText" onclick="addText()">Add Text</button><br>
我试图让这个按钮调用这个函数来更新一个文本字段。我不希望它进入操作页面或任何东西。显示方面甚至都不重要。我只是想找出如何通过单击按钮在后台更新变量。
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";
}
推荐阅读
- javascript - 禁用 v-autocomplete 中的选项
- ruby-on-rails - 尝试显示图像时,出现错误“404 not found”
- assembly - AVR 汇编:jmp 和 rjmp 指令中的地址是什么?
- arrays - 解析 json 数据并在表格中循环显示
- loops - 从单个数据集创建多个 SAS 宏变量列表
- javascript - 使用javascript在chrome或firefox(均为最新版本)上自动打印pdf文件
- macos - macOSX 上“LWP::Protocol::https 未安装”的哪个自制软件包?
- r - `as.Date()` 返回 `NA` 而不是字符串“2012-01”的日期对象
- javascript - 在 Lodash 中从字符串数组转换为 hashmap
- javascript - 什么是 JavaScript 中的“导出”?