鱼C论坛

 找回密码
 立即注册
查看: 1984|回复: 2

[庖丁解牛] 0 1 0 1 ★ history对象 属性 & 方法大全

[复制链接]
发表于 2017-4-21 10:53:25 | 显示全部楼层 |阅读模式
购买主题 已有 1 人购买  本主题需向作者支付 3 鱼币 才能浏览

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-12 09:50:09 | 显示全部楼层
本帖最后由 a211827754 于 2018-9-12 09:52 编辑

history.forward()和history.go()可以演示的,用loaction.assign("https://www.baidu.com")或者location.replace("http://www.fishc.com")产生历史记录,然后再退回来就可以操作了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-27 16:26:40 | 显示全部楼层
学习了
按我的理解写了一个 模拟历史列表的例子 希望能帮到世界上下一个看到这的人吧
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.   <title>History Example</title>
  5. </head>

  6. <body>
  7.   <button onclick="history.back()">返回</button>
  8.   <button onclick="history.forward()">前进</button>
  9.   <button onclick="history.go(-1)">使用Go(-1)方法返回</button>
  10.   <button onclick="addHistory()">模拟访问一个网址</button>
  11.   <button onclick="replaceHistory()">替换列表中当前记录</button>

  12.   <p>当前页面:<span id="current-page">Home</span></p>
  13.   <p>历史记录长度:<span id="history-length">0</span></p>

  14.   <script>
  15.     // 更新页面显示的当前 URL 和历史记录长度
  16.     function updateDisplay() {
  17.       document.getElementById("current-page").innerText = window.location.href;
  18.       document.getElementById("history-length").innerText = history.length;
  19.     }

  20.     // 模拟添加新的历史记录 小巧思之36进制包含数字与英文的随机字符串
  21.     function addHistory() {
  22.       history.pushState({}, "New Page", "?page=" + Math.random().toString(36).substring(7));
  23.       updateDisplay(); // 更新显示
  24.     }

  25.     // 替换当前的历史记录
  26.     function replaceHistory() {
  27.       history.replaceState({}, "Replaced Page", "?replaced=" + Math.random().toString(36).substring(7));
  28.       updateDisplay(); // 更新显示
  29.     }

  30.     // 监听浏览器的 "popstate" 事件,用于处理返回和前进操作
  31.     window.addEventListener("popstate", function () {
  32.       updateDisplay(); // 当用户返回或前进时,更新显示内容
  33.     });

  34.     // 初始化页面显示
  35.     updateDisplay();
  36.   </script>
  37. </body>

  38. </html>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 20:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表