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

标签 javascript jquery jquery-ui

这是数组

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]);
});

我错过了什么?谢谢。

最佳答案

首先,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]);
  });

});

jsFiddle example

关于javascript - 使用 Javascript/Jquery 在单击按钮时更改 div 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295227/

相关文章:

jquery js 和 jquery mobile js 不工作

jquery - 添加CSS时出现奇怪的效果

jquery ui 选项卡不工作

javascript - 通过在参数中传递停止函数来清除间隔?

javascript - 扩展 typescriptdefineTyped 定义文件

javascript - Vue列表排序后不重新渲染 "properly"

jquery - document.ready 中声明的函数未定义?

asp.net - jquery通过客户端函数选择radiobuttonlist中的项目

javascript - Jquery 自动完成不附加

javascript - Webpack 2.4.1 中的无效配置对象错误