![](/img/trans.png)
[英]Replacement for jQuery's: $('#id').html(HTMLfragment) // incl. <script> execution
[英]HTML id block replacement
我正在學習cshtml / js,但不確定如何替換文本塊。 這是我的代碼:
<!DOCTYPE html>
<html>
<head>
<p id = "Jonny">Jonny the giant </p>
<p> hi there <b id ="mytext"> dood! </b></p>
<script>
function Replace() {
y = document.getElementById("mytext");
y.innerHTML = "new string"; @* what I want to do --> document.getElementById("Jonny");*@
}
</script>
<button
type = "button" onclick = "Replace()" > 'MAGIC SWAP'
</button>
</body>
</html>
我想用我用ID名稱標識的另一段文本替換該段文本
看起來這就是您所追求的。 document.getElementById("mytext").innerHTML = document.getElementById("Jonny").innerHTML;
這是HTML
的清理版本。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function Replace() {
document.getElementById("mytext").innerHTML = document.getElementById("Jonny").innerHTML;
}
</script>
</head>
<body>
<p id="Jonny">Jonny the giant</p>
<p>Hi there <b id="mytext"> dood! </b></p>
<button type="button" onclick="Replace()">MAGIC SWAP</button>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.