使用Javascript/Jquery在按钮单击时更改div中的文本

win*_*f88 3 javascript jquery jquery-ui

这是阵列

var copyText= [
'this is the first line',
'Simply put, the second line',
'impressed by the third line.'
    ];
Run Code Online (Sandbox Code Playgroud)

这些不起作用......

$("#thisbutton").click(function () {
$("#thetext").innerHTML("meow meow");
});
Run Code Online (Sandbox Code Playgroud)

要么

$("#thisbutton").click(function () {
$("#thetext").innerHTML(copyText[1]);
});
Run Code Online (Sandbox Code Playgroud)

要么

$("#thisbutton").click(function () {
$("#thetext").text(copyText[1]);
});


$("#thisbutton").click(function () {
$("#thetext").html(copyText[1]);
});
Run Code Online (Sandbox Code Playgroud)

我错过了什么?THKS.

Jac*_*kin 6

首先,innerHTML是一个本地DOMElement财产,而不是一种jQuery方法. jQueryhtml方法是等效的.

其次,你没有将它包装在一个ready处理程序中:

试试这个:

$(function() {
  var copyText= [
   'this is the first line',
   'Simply put, the second line',
   'impressed by the third line.'
  ];
  $("#thisbutton").click(function () {
     $("#thetext").text(copyText[1]);
  });

});
Run Code Online (Sandbox Code Playgroud)

jsFiddle例子