gpt4 book ai didi

javascript - 使用两个不同的 js 从一个 HTML 页面发送到另一个

转载 作者:行者123 更新时间:2023-11-30 13:59:17 26 4
gpt4 key购买 nike

我在主页上使用 HTML 和 JS 将变量发送到另一个 js 页面。

这是我做的在页面索引中,我使用 index.js 创建按钮

<a href="index.html" id= "button1">Easy</a>

在 index.js 中我这样做了

window.addEventListener('load', function(){
document.getElementById("button1").onclick = function(){
sessionStorage.setItem("mood", '1');
window.location.replace("second.html");
};

在 second.js 中,我在 js 中做了这个来获取情绪变量。

data = sessionStorage.getItem('mood');

在 Firefox 上一切正常(在本地),但是当使用 Chrome 或在线上传时按钮不起作用,因为它不会重定向到 second.html

谁能帮帮我?p.s 我不想使用脚本(Ajax、Jquery 等)

最佳答案

如我的评论所述,将 href 设置为“#”,或将其从 a 标签切换为按钮标签将防止双页直接显示,而是运行 onclick 函数,设置您的存储然后重定向到来自 index.html 的 second.html。

例子:

<a href="#" id="button1">Easy</a>

<button id="button1" onclick="loadB1()" >Easy</button>

并以这种方式在 index.js 中调用一个函数:

function loadB1(){
sessionStorage.setItem("mood", '1');
window.location.replace("second.html");
};

关于javascript - 使用两个不同的 js 从一个 HTML 页面发送到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56645028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com