gpt4 book ai didi

javascript - 如何在javascript中的不同html页面之间传递变量值

转载 作者:太空狗 更新时间:2023-10-29 13:54:15 27 4
gpt4 key购买 nike

我想将选定的列表项的值传递到另一个页面,这意味着如果我从列表中选择 abc,那么这个 abc 值将传递到下一个 html 表单,它应该打开该配置文件仅页面。有什么方法可以在不同的 html 页面中使用此变量。

$('.ui-li-icon li').click(function() {
var index = $(this).index();
text = $(this).text();
alert('Index is: ' + index + ' and text is ' + text);

我想将上面的文本值传递给我的 profile.html,它具有 javascript 函数 profile()。所以我想在函数调用中传递此文本,例如profile(text);我尝试在函数调用上方声明 var text 但仍然无法正常工作。请告诉我是否有其他方法。

最佳答案

您可以将值作为 url 片段传递。

在点击功能中,打开 '/profile.html#'+text

在您的 profile.html 中获取 url 片段。

示例代码:

导航到 profile.html

window.location.href = '<path to profile.html>' + '#' + text;

在profile()中,要获取参数,使用

var text = window.location.hash.substring(1)

关于javascript - 如何在javascript中的不同html页面之间传递变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23213788/

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