我正在嘗試一個相當簡單的事情。我想在用戶點擊它時替換h2標籤內的文本。我的代碼只是添加一個額外的h2元素,保持原來的內容。無法使用jquery替換h2標記中的內容
<?DOCTYPE HTML>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var html = "";
var value = "New Text "
$("#sample").click(function(){
if(value == "Old Text"){
value = "New Text";
} else {
value ="Old Text";
}
html += "<h2 id='target'>"+value+"</h2>";
$("#target").html(html);
});
});
</script>
</head>
<body>
<div id ="sample">
<h2 id = "target"> Change this Text </h2>
</div>
</body>