如何带参数跳转至本地HTML文件?求解决方案
问题解决方法
1. 修正跳转URL格式
你之前的写法错误地把参数嵌入了文件名中,正确的URL格式应该是文件名 + 分隔符? + 参数键值对,正确代码如下:
window.location.href = `detail.html?id=${countryCode}`;
2. 在detail.html中获取参数
不需要额外JS库,用原生JS的URLSearchParams就能轻松获取参数:
// 在detail.html的脚本中添加这段代码 const searchParams = new URLSearchParams(window.location.search); const countryCode = searchParams.get('id'); // 示例:打印参数值 console.log(countryCode);
3. 特殊字符处理(可选)
如果countryCode包含空格、&、=等特殊字符,需要先编码避免URL解析错误:
window.location.href = `detail.html?id=${encodeURIComponent(countryCode)}`;
获取时无需手动解码,URLSearchParams.get()会自动处理解码。
内容的提问来源于stack exchange,提问作者Raziel
相关产品推荐
相关产品推荐

