zoukankan      html  css  js  c++  java
  • pushstate

    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <title>History Api</title>
    <style>
    html,
    body {
    height: 100%;
    overflow: hidden;
    margin: 0;
    padding: 0;
    }

    aside {
    background-color: #ccc;
    220px;
    float: left;
    height: 100%;
    }

    aside ul {
    font-size: 20px;
    line-height: 2;
    }

    aside ul li {
    cursor: pointer;
    }

    article {
    background-color: #f5f5f5;
    margin-left: 220px;
    padding: 20px;
    height: 100%;
    overflow: scroll;
    font-size: 20px;
    }
    </style>
    </head>

    <body>
    <aside>
    <ul id="list" data-id="1" data-name="sss">
    </ul>
    </aside>
    <article>
    <p id="content"></p>
    </article>
    <!-- plugin:auto file name -->
    <script src="data.js"></script>
    <script>
    (function() {
    var listElement = document.querySelector('#list');
    for (var title in data) {
    var liElement = document.createElement('li');
    liElement.innerHTML = '⭐️' + title;
    liElement.setAttribute('data-title', title);
    listElement.appendChild(liElement);
    }

    var liElements = document.querySelectorAll('#list>li');

    var content = document.querySelector('#content');
    // 注册美哦一个元素事件
    for (var i = 0; i < liElements.length; i++) {
    liElements[i].addEventListener('click', function() {
    // 拿到被点击title
    var title = this.dataset['title'];
    // 赋值
    content.innerHTML = data[title];
    // 操作历史记录
    if (window.history && history.pushState) {
    // 添加一个新的历史记录
    history.pushState(title, 'title没有任何浏览器支持', '?t=' + title);
    } else {
    console.log('不支持');
    }
    });
    }

    // 当我们在伪造的访问历史中前进或后退时会执行一个popstate事件
    window.addEventListener('popstate', function(e) {
    content.innerHTML = data[e.state];
    });

    // window.location = "https://www.baidu.com";
    // 第一次请求过来 获取地址栏中的t参数
    // window.location可以拿到当前网页中跟地址相关的信息
    var search = window.location.search; // ?t=jkaljdksfla
    // 如果地址栏中的地址有中文,会以URL编码方式呈现
    // decodeURI 可以转换到之前中文
    var title = search.split('=')[1]; // ['?t','jkaljdksfla']
    if (title) {
    // 有值 decodeURI作用就是从URL编码转换到之前的状态
    console.log(decodeURI(title));
    content.innerHTML = data[decodeURI(title)];
    }
    })();
    </script>
    </body>

    </html>

  • 相关阅读:
    L2 L3 L4
    C 语言assert使用
    VIM 命令收藏
    C++继承实例
    关于 WinScp 的一点使用经验
    Boa服务器移植
    Android 去掉标题全屏显示
    sys下gpio操作
    linux下 XGCOM串口助手的安装
    linux中inittab文件详解
  • 原文地址:https://www.cnblogs.com/wyq-web/p/9496241.html
Copyright © 2011-2022 走看看