页面刷新后如何更改innerHTML?

时间:2018-03-23 16:21:55

标签: javascript html user-input innerhtml page-refresh

当您在输入字段中输入任何内容时,它将按预期替换(更改),并且我不知道在页面重新加载后它们如何保持这种方式(基于输入值更改)...我尝试了很多选项从这里和其他论坛形成,但没有任何帮助。请帮助我成为这个东西的新手。

var savea = document.getElementById("save3");
if (savea) {
  savea.addEventListener("click", saveea);
}

var saveb = document.getElementById("save4");
if (saveb) {
  saveb.addEventListener("click", saveeb);
}

function saveea() {
  var changename = document.getElementById("savenamemovie").value;
  document.getElementById("namemovie1").innerHTML = changename;
}

function saveeb() {
  var changegenre = document.getElementById("savegenremovie").value;
  document.getElementById("genremovie1").innerHTML = changegenre;
}
<header>
  <div class="container">
    <div id="branding">
      <h1><span id="logo">mov</span>BLANK</h1>
    </div>
    <nav>
      <ul>
        <li><a href="../index.html">Home</a></li>
        <li><a id="newprojection" href="../html/newprojection.html">New projection</a></li>
        <li><a id="buyticket" href="../html/buyticket.html">Buy a ticket</a></li>
        <li><a id="newuser" href="../html/newuser.html">New user</a></li>
        <li><a id="loginbtn" href="../html/login.html">Log in</a></li>
        <li><a id="buy2" href="#">admin</a></li>
      </ul>
    </nav>
  </div>
</header>

<section id="boxes">
  <div class=".container">
    <div id="annihilation" class="moviebox">
      <a class="moviea"><img src="../img/movie1.jpg"></a>
      <h3 id="namemovie1">Annihilation</h3>
      <input id="savenamemovie" type="text" name="movietitle"><a id="save3" class="save2">SAVE</a>
      <p>Genre: <span id="genremovie1">Adventure, Fantasy</span></p>
      <input id="savegenremovie" type="text" name="movietitle"><a id="save4" class="save2">SAVE</a>
    </div>
    <div class="trailer">
      <embed src="https://www.youtube.com/embed/89OP78l9oF0" allowfullscreen="true">
    </div>
    <div class="moviebox">
      <p>Ticket Price:<span id="pricemovie1" class="boje"> 250 RSD</span></p>
      <p>Duration:<span id="durationmovie1" class="boje"> 147 min.</span></p>
      <p>Date:<span id="datemovie1" class="boje"> 25/06/18</span></p>
      <p>Time:<span id="timemovie1" class="boje"> 18:30</span></p>
      <p>Place:<span id="placemovie1" class="boje"> Arizona, Springfiled 12</span></p>
      <p>Theater:<span id="theatre1" class="boje"> A1</span></p>
      <br>
      <a id="delete">Free: 14 seats</a>
      <a id="change">Movie rated: 7.8</a>
      <a id="buy" class="buttons" href="buyticket.html">Buy a Ticket</a>
    </div>
  </div>
</section>

1 个答案:

答案 0 :(得分:1)

您可以将您的值存储在localStorage中,并在keyup上更新,以始终保持正确的值。

var input = document.getElementById("savenamemovie");

if (input) { 
    input.value = localStorage.getItem('savenamemovie') || "";
    input.addEventListener("keyup", function(){
        localStorage.setItem('savenamemovie',this.value);
    }); 
}