0

我正在学习 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 名称替换该块文本

4

1 回答 1

1

看起来这就是你所追求的。 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>
于 2013-08-28T01:04:52.967 回答