微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

保留div的内容

如何解决保留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>
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。