繁体   English   中英

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

[英]Using Javascript / Jquery to change text in div upon button click

这是阵列

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方法。 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例子

这对我有用......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <div id="thetext">

    </div>
    <input type="button" id="thisbutton" value="Click" />
<script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $("#thisbutton").click(function () { $("#thetext").html("meow meow"); });
    });
</script>
</body>
</html>

如果要替换完整的div,请尝试使用replaceWith()jquery函数

 $("#thetext").replaceWith("htmldata");

参考: http//api.jquery.com/replaceWith/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM