这是我使用 javascript 做任何事情的第二天,所以我是一个完全的初学者。正如标题所述,我只想使用 settimeout 函数更改文本框的文本值。我在互联网上进行了搜索,却陷入了死胡同。这是我到目前为止所拥有的,
putftp.onclick = function () {
var Text = document.getElementById("TextBox");
function firsttext() {
document.getElementbyID("TextBox").innerHTML = "This is the first test.";
setTimeout("secondtest()",3000);
}
function secondtest() {
document.getElementById("TextBox").innerHTML = "This is the second test.";
setTimeout("thirdtest()",5000);
}
function thirdtest() {
document.getElementById("TextBox").innerHTML = "This is the last test.";
}
};
我不确定我是否使用了正确的格式,或者我是否接近正确。我很确定除了 document.getElementbyID("textbox").innerHTML 部分之外一切都很好。我认为会有一些改变,但这只是我的第二天,所以我真的对整个问题一无所知。提前感谢您的帮助!
最佳答案
要在单击按钮后 3 秒更改一次文本,请执行以下操作:
putftp.onclick = function () {
window.setTimeout(function() {
document.getElementById("TextBox").value = "This is the first test.";
}, 3000);
};
您的原始代码中有两个错误,我已为您修复:
- 假设
TextBox
是一个文本框,您需要为其分配value
属性,而不是innerHTML
。 - 正确的名称是
getElementById
,而不是getElementbyID
。 JavaScript 区分大小写。
要在两秒后再次更改它,您可以添加“嵌套”计时器:
putftp.onclick = function () {
window.setTimeout(function() {
document.getElementById("TextBox").value = "This is the first test.";
window.setTimeout(function() {
document.getElementById("TextBox").value= "This is the second test.";
}, 2000);
}, 3000);
};
关于javascript - SetTimeout 函数更改文本框中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7982243/