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

这是阵列

var copyText= [
'this is the first line','Simply put,the second line','impressed by the third line.'
    ];

这些不起作用……

$("#thisbutton").click(function () {
$("#thetext").innerHTML("meow meow");
});

要么

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

要么

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


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

我错过了什么? THKS.

解决方法

首先,innerHTML是本机DOMElement属性,而不是jQuery方法.
jQuery的html方法是等价的.

其次,你没有将它包装在一个准备好的处理程序中:

试试这个:

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

});

jsFiddle example

相关文章

1.第一步 设置响应头 header('Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...