保留div的内容

| 我有一个div,其内容名为
level0
<div id=\"level0\">
)。不幸的是,我的网页中的“ 0”发生了变化,我需要以相同的第一个值重新使用“ 3”。 我该如何实现? 编辑 我的代码:
<body onload =\"init()\">
          <h1>Search engine bêta version</h1>
      <input id=\"text\" type=\"text\" size=\"60\" value=\"Type your keywords\" />
      <input type=\"button\" value=\"Display the results\" onclick=\"check();\" />

      <script =\"text/javascript\">

      function check() {
          var div = document.getElementById(\"level0\");   // Here level0 takes the value of Type your keywords and I want it to stick with the first value
          div.innerHTML = document.getElementById(\"text\").value;
      }

     </script>

      <div id=\"level0\"> // The value I want to keep
      </div>

</body>
    
已邀请:
如果要使用显示结果的第一个\“ text \”,则此代码可能有用           搜索引擎bêta版本              
  <script =\"text/javascript\">
var i=1;
var firstText;
  function check() {
  if(i==1)
  {
    firstText=document.getElementById(\"text\").value;
  }
      var div = document.getElementById(\"level0\");   // Here level0 takes the value of Type your keywords and I want it to stick with the first value
      div.innerHTML = document.getElementById(\"text\").value;
      i++;
      alert(firstText);
  }

 </script>

  <div id=\"level0\"> // The value I want to keep
  </div>
    
下面的代码应为您解决此问题。第一次替换时,它将存储原始内容,然后每次在div中更改文本时,它将在原始内容之前添加。
<body onload =\"init()\">
      <h1>Search engine bêta version</h1>
      <input id=\"text\" type=\"text\" size=\"60\" value=\"Type your keywords\" />
      <input type=\"button\" value=\"Display the results\" onclick=\"check();\" />

      <script =\"text/javascript\">
      //Whether or not we\'re replacing the content in the div for the first time
      var firstReplace = true;
      //The original content of the div
      var originalContent;
      function check() {
        var div = document.getElementById(\"level0\");   // Here level0 takes the value of Type your keywords and I want it to stick with the first value
        //Check if this is the first time
        if (firstReplace) {
            //Is the first time, so store the content
            originalContent = div.innerHTML;
            //Set firstReplace to false, so we don\'t overwrite the origianlContent variable again
            firstReplace = false;
        }
        div.innerHTML = originalContent + \' \' + document.getElementById(\"text\").value;
      }
     </script>

      <div id=\"level0\"> // The value I want to keep
      </div>

</body>
    

要回复问题请先登录注册