保留div的内容

发布于 2024-11-01 09:59:08 字数 900 浏览 1 评论 0原文

我有一个 div,其内容名为 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>

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 to keep
      </div>

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒带 2024-11-08 09:59:08

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

搜索引擎测试版

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

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>

不醒的梦 2024-11-08 09:59:08

下面的代码应该可以为您解决这个问题。第一次替换时,它会存储原始内容,然后每次更改 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文