javascript - 如果用户刷新页面,是否可以维护 jquery 事件显示的内容?

标签 javascript jquery html css

在我的网页中,我通过使用 jquery 单击导航菜单来更改内容(如 gmail)。这是例子,

    <p>
    I have a classifieds website, and on the page where ads are showed, 
    I am creating a "Send a tip to a friend" form... So anybody who wants can send a tip of the ad to some friends email-adress. 
    I am guessing the form must be submitted to a php page right? When submitting the form, the page gets reloaded... I don't want that... Is there any way to make it not reload and still send the mail? Preferrably without ajax or jquery... Thanks
    </p>
    <button id="chg">
    Change content
    </button>

这是我用 jquery 尝试的,

$(document).ready(function(){
  var isnew = "";
    console.log(isnew);
        if(isnew == true) {
            console.log("check "+isnew);
            $("p").html("New contents are here........");
        }

    $("#chg").click(function(){
        isnew = true;
        console.log(isnew);
        $("p").html("New contents are here........");
    });
});

我想要的要点是,在我点击 change content 按钮后,现在 p 元素中的文本更改为 New contents are here.. ...

此时,如果用户重新加载页面,我只想显示更改的内容,如 New contents are here.....。不是用 html 编写的文本(段落)。

我已经尽力了。但我无法解决问题。因此,我非常感谢任何建议。

最佳答案

$(document).ready(function(){
    if (sessionStorage.getItem("newContent")) {
     // Restore the contents
     $("p").html(sessionStorage.getItem("newContent"));
    }
    $("#chg").click(function(){
        var newCont = "New contents are here........";
        $("p").html(newCont);
        sessionStorage.setItem("newContent", newCont);
    });
});

Reference

check usage

关于javascript - 如果用户刷新页面,是否可以维护 jquery 事件显示的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411164/

相关文章:

javascript - 我有一个从 HTML 页面接受值的 JS 文件。我必须在嵌入 JS 的 CSS 中使用这个变量

javascript - Highcharts饼图隐藏零扇区

javascript - 语义 ui react 表 GridView float 卡问题

javascript - 如何在没有提交按钮的情况下使用回车键在表单中发送 <select>

html - 导航栏水平+垂直居中

javascript - 使用 javascript(或 jQuery)选择和操作 CSS 伪元素,例如::before 和::after

javascript - 验证是否选择了文本(在 div 中)

javascript - 重新加载 jquery 和 js 库

javascript - 我们如何为鼠标滚轮敏感的 <div> 制作样式滚动条

javascript - 将属性后的 css 转换为 jquery