javascript - 当用户点击 div 时导航到带有附加查询的新 url
问题描述
我正在尝试完成以下操作:单击 div 时,用户将转到新的 url,并将查询字符串添加到 url。
例如,用户在这个 url 上:
www.example.com/step1/?query1=abc&query2=def
当一个 div 被点击时,用户应该去:
www.example.com/step2/?query1=abc&query2=def&query3=ghi
解决方案
尝试这个:
function go(){
var currentUrl = window.location.href;
var targetUrl = 'http://example.com/?query3=value';
var newUrl = currentUrl + "&" + targetUrl.split("?")[1];
console.log(newUrl);
//window.location.href = newUrl;
}
<div onclick="go()">GO</div>
推荐阅读
- python - 使用键和值作为子列表反转字典
- android - 某些文件已损坏
- git - 如何添加子模块作为对存储库的引用?
- android - Firebase 云消息传递。令牌检索失败:SERVICE_NOT_AVAILABLE
- c - 如何动态获取整数输入并在按 Enter 时终止循环?
- microsoft-cognitive - 为什么表单识别器返回的“boundingBox”坐标对于我的 PDF 表单似乎不正确?
- sql-server - 执行 SQL 任务的行为不同的加载开始日期和结束日期
- python - gRPC 客户端无法使用 TLS 证书连接到服务器
- android - 什么是向 Google 游戏服务注册的服务器应用程序形式的 Android 游戏中的启动 URL
- python - 插入不可调用的 ORM 表