gpt4 book ai didi

javascript - 获取 html url 变量

转载 作者:行者123 更新时间:2023-11-28 01:29:56 24 4
gpt4 key购买 nike

我正在使用下面的代码将变量发送到另一个页面,但我得到“未定义”

<script type="text/javascript">
$(document).ready(function() {

$('.letterz').click(function () {
var selectedletter = $(this).attr("id");
$("#celebdetails").load("reveal.html?surname="+selectedletter, function() {
$('.myloader').fadeOut("fast");
$('#celebdetails').fadeIn("slow");
});
});

});
</script>

我的html

<div class="myloader"></div>

<div id="#celebdetails" style="display:none;"></div>

<div class="letterz" id="A"></div>
<div class="letterz" id="B"></div>
<div class="letterz" id="C"></div>
<div class="letterz" id="D"></div>

在我发送参数姓氏的第二页中,我使用下面的代码来获取它:

<script type="text/javascript">
$(document).ready(function() {

function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}

var me = getUrlVars()["surname"];
alert(me);

});
</script>

提前致谢

最佳答案

尝试一下:

function geturlvar() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

这样调用它:

 var as = geturlvar()['urlvar'];

所以网址:http://stackoverflow.com?result=202

然后:var myvar = geturlvar()['result'];

myvar contains : 202;

关于javascript - 获取 html url 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22247492/

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