首页 > 解决方案 > 将当前 URL 参数附加到锚链接上

问题描述

在像这样的页面

https://www.example.com/?firstname=Steven&lastname=Smith&email=steve%40gmail.com&phone=0404555555

我有一个#ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell链接到的按钮(锚链接)https://www.example.com/form

我想将当前 URL 中的 URL 参数附加到按钮的 URL,以便按钮的 href 现在是https://www.example.com/form/?firstname=Steven&lastname=Doig&email=steve%40gmail.com&phone=0404555555

请问如何用 JavaScript 做到这一点?

标签: javascript

解决方案


使用window.location.search

var url = "https://exmaple.com";
var newurl = url + window.location.search;

newurl将包含所有get(例如?something=something&something2=something5)数据。

要更改href:a

var button = document.getElementById('#ptsBlock_553944');
button.href = button.href + window.location.search;

推荐阅读