保留div的内容

时间:2011-04-14 09:58:18

标签: html xhtml w3c

我有一个div,其内容名为level0<div id="level0">)。遗憾的是level0我的网页发生了变化,我需要重复使用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">

      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>

2 个答案:

答案 0 :(得分:0)

如果想要使用显示结果的第一个“文本”,则此代码可能很有用

          

搜索引擎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>

答案 1 :(得分:0)

以下代码可以为您解决此问题。在它第一次替换时,它将存储原始内容,然后每次更改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>