开发者

Keep the content of a div

I've got a div which has a content named level0 (<div id="level0">). Unfortunately level0 changes in my webpage and I need the reuse the div with the same first value.

How can I achieve this?

Edit

My code:

<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 t开发者_开发技巧o keep
      </div>

</body>


If want to use the first "text" for which you had displayed results this code may be useful

Search engine bêta version

  <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>


The code below should solve this for you. On the first time it replaces, it will store the original content, and then each time it changes the text in the div, it will prepend it with the original content.

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜