[英]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
方法。 jQuery
的html
方法是等價的。
其次,你沒有將它包裝在一個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]);
});
});
這對我有用......
<!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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.