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

时间:2010-11-28 03:42:03

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

我缺少什么? THKS。

3 个答案:

答案 0 :(得分:6)

首先,innerHTML是本机DOMElement属性,而不是jQuery方法。 jQuery的{​​{1}}方法就是等价的。

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

试试这个:

ready

<强> jsFiddle example

答案 1 :(得分:1)

这对我有用......

<!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>

答案 2 :(得分:0)

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

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

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