1

我的页面上有一个按钮。我希望该按钮每 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
4

4 回答 4

2

这是解决您的问题的最强大和最简单的解决方案。JSFIDDLE。循环使用预定义的语言词典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);
于 2013-10-23T16:50:58.123 回答
2
> 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";
    }
}
于 2013-10-23T17:12:19.617 回答
1

使用setInterval.

setInterval(function() {
    var btn = document.getElementById('search');
    if (btn.innerHTML == "search")
         btn.innerHTML = "Suchen";
    else
         btn.innerHTML = "search";
   }, 2000);
于 2013-10-23T16:50:01.397 回答
0

您还可以将按钮更改为 aninput并使用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" />
于 2013-10-23T17:06:11.330 回答