0

HTML DOMへの書き込みに使用できるJavascriptコードは何ですか?クリックするたびにドキュメント要素に異なるテキストを書き込むボタンを作成したいですか?例;

最初のクリック:

document.getElementById("paragraph").innerHTML= "This is is the initial text."

2回目のクリック:

document.getElementById("paragraph").innerHTML= "This text replaces the initial text"

3回目のクリック:

document.getElementById("paragraph").innerHTML= "This text replaces the text from the second click"

などなど...

事前に感謝せずにすべて。

4

2 に答える 2

0

すべてのテキストオプションを配列に追加します。

クリックするたびにカウントアップする変数を設定します。

カウンターの番号を使用して、配列からテキストを呼び出します。

いくつかのコード:

var counter = 0;
var theArray = new Array("This is is the initial text.", "This text replaces the initial text", "This text replaces the text from the second click");

function clicked(){
   document.getElementById("paragraph").innerHTML= theArray[counter++];
}
于 2012-06-13T10:47:25.900 に答える
0

使用可能なすべてのテキスト行の配列を作成します。

次に、ボタンをクリックして、乱数を生成し、配列からそのインデックス値に置き換えます。

var contentArray = ["This is is the initial text.", "This text replaces the initial text",    "This text replaces the text from the second click"],
length = countArray.length;

$(btn).bind('click',function(){
  var randomNumber = Math.floor(Math.random()*length);
  document.getElementById("paragraph").innerHTML = contentArray[randomNumber];
});
于 2012-06-13T10:49:59.457 に答える