如何在 Javascript 中随时间更改按钮文本
How to change button text over time in Javascript?
我的页面上有一个按钮。我希望这个按钮每 2/4 秒使用 Javascript 更改一次语言。例如,当页面加载时,按钮的文本将被搜索,2或4秒后它将更改为其他语言。它不需要是一个无限循环,只需要最简单的循环。
.HTML:
<button id="search" name="q">search</button>`
Javascript:
var x = document.getElementById('search');
//after 2 seconds:
x.innerHTML="Suchen";
//And so on
这是针对您的问题的最强大和最简单的解决方案。吉斯菲德尔。使用 setInterval()
遍历预定义的语言词典
var x = document.getElementById('search'),
// dictionary of all the languages
lan = ['Search', 'Suchen', 'other'],
// hold the spot in the dictionary
i = 1;
setInterval(function (){
// change the text using the dictionary
// i++ go to the next language
x.innerHTML = lan[i++];
// start over if i === dictionary length
i = lan.length === i ? 0 : i;
}, 2000);
> Demo : http://jsfiddle.net/JtHa5/
.HTML
<button id="search" name="q">Search</button>`
Javascript:
setInterval(changeButtonText, 2000);
function changeButtonText()
{
var btnTxt = document.getElementById('search');
if (btnTxt.innerHTML == "Search"){
btnTxt.innerHTML = "Suchen";
}
else{
btnTxt.innerHTML = "Search";
}
}
使用 setInterval
。
setInterval(function() {
var btn = document.getElementById('search');
if (btn.innerHTML == "search")
btn.innerHTML = "Suchen";
else
btn.innerHTML = "search";
}, 2000);
还可以
将按钮更改为input
并使用 value
属性而不是 innerHTML
属性。这是Javascript:
function changeButton() {
var btn = document.getElementById('myButton');
if (btn.value == "Search")
btn.value = "Suchen";
else
btn.value = "Search";
}
setInterval(changeButton, 2000);
和 HTML
<input type="button" id="myButton" value="Search" />
相关文章:
- 我想重定向点击,然后更改按钮文本,我该怎么做
- 使用JavaScript将标题文本替换为按钮文本
- 将按钮文本从“;喜欢“;至“;喜欢“;单击时
- 如何在jQuery mobile中更改按钮文本
- 不便更改Javascript中的按钮文本
- 为什么我的导航按钮或按钮文本不会移动到中心
- 根据单选按钮值更改提交按钮文本
- 更改按钮文本
- 按钮文本中的新行
- 在呈现 D3 SVG 对象之前更改引导按钮文本
- 制作一个“;输入“;按钮文本不可编辑
- 通过引导程序加载/重置后更改按钮文本
- 动态创建的按钮文本
- jQuery移动列表过滤:如何更改清除按钮文本
- 如何使用JavaScript在点击时更改按钮文本
- sap.m.按钮文本格式化程序
- AngularJS按钮文本不起作用
- 如何在单击时更改按钮文本
- 使用Jquery仅切换按钮文本中的ASCII字符
- TinyMCE 编辑输入值和按钮文本