HTML id块替换

时间:2013-08-27 22:39:47

标签: javascript html5 razor

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

1 个答案:

答案 0 :(得分: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>