首页 > 解决方案 > 当用户点击 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

标签: javascriptjquery

解决方案


尝试这个:

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>


推荐阅读