[英]Using Javascript / Jquery to change text in div upon button click
here is the array 这是阵列
var copyText= [
'this is the first line',
'Simply put, the second line',
'impressed by the third line.'
];
These don't work ... 这些不起作用......
$("#thisbutton").click(function () {
$("#thetext").innerHTML("meow meow");
});
or 要么
$("#thisbutton").click(function () {
$("#thetext").innerHTML(copyText[1]);
});
or 要么
$("#thisbutton").click(function () {
$("#thetext").text(copyText[1]);
});
$("#thisbutton").click(function () {
$("#thetext").html(copyText[1]);
});
What I am missing? 我错过了什么? thks.
THKS。
First of all, innerHTML
is a native DOMElement
property, not a jQuery
method. 首先,
innerHTML
是本机DOMElement
属性,而不是jQuery
方法。 jQuery
's html
method is the equivalent. jQuery
的html
方法是等价的。
Secondly, you are not wrapping this in a ready
handler: 其次,你没有将它包装在一个
ready
处理程序中:
Try this: 试试这个:
$(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]);
});
});
This worked for me... 这对我有用......
<!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>
If you want to replace the complete div try replaceWith() jquery function 如果要替换完整的div,请尝试使用replaceWith()jquery函数
$("#thetext").replaceWith("htmldata");
Reference: http://api.jquery.com/replaceWith/ 参考: http : //api.jquery.com/replaceWith/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.