【问题标题】:Changing header content with cookies使用 cookie 更改标题内容
【发布时间】:2018-05-11 11:29:09
【问题描述】:

我需要帮助更改标题中的文本内容。用户在第一页输入名称,它应该将下一页上的 h2 更改为键入的任何内容。任何帮助将不胜感激,是的,我添加了 Cookie.js 库。 这是我目前所拥有的:

入口页面:

<form action="Lab12-p2.html" method="post">
  <h3>Enter Name </h3>
  <label for="name">Username: </label>
  <input id="name" type="text" name="name" />
  <input id="submit2" type="submit" value="Submit" />
</form>

第 2 页:

<h2 id ="username"> Hi there! </h2>

Javascript:

 $("#name").blur(function(event) {
   var test = event.target.value;
   Cookie.set("content", test);
   var ger = Cookie.get("content");
   $("#username").html("Hi" + content);
 });

【问题讨论】:

    标签: javascript jquery html css cookies


    【解决方案1】:

    在第 2 页添加脚本

    $("document").ready(function() {
       var ger = Cookie.get("content");
       $("h2").html("Hi" + content);
     });
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 html5,您也可以这样做

      $("#name").blur(function(event) {
         var test = event.target.value;
         localStorage.setItem("content", test);  
         $("#username").text("Hi " + localStorage.getItem("content"));
       });
      

      https://jsfiddle.net/hjqre6ru/

      【讨论】:

      • 您如何使用 HTML5。有没有办法用 cookie 做到这一点?我什至接近吗?
      • 上述解决方案无需任何 js 库即可工作。你也可以使用你的代码 var ger = Cookie.get("content"); $("#username").html("Hi" + ger);
      • localStorage 像浏览器数据库一样保存“键值”数据
      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多