gpt4 book ai didi

javascript - 在 Javascript 事件上更改 URL(在 HTML 内)中的变量

转载 作者:行者123 更新时间:2023-12-02 17:56:44 25 4
gpt4 key购买 nike

我有一个 Javascript 事件,当检测到位置时会触发该事件。假设检测到的位置是“澳大利亚墨尔本”。

我想更改以下内容:

<a href="/search?location=&input=Suits">Suits</a> 

成为:

<a href="/search?location=Melbourne Australia&input=Suits">Suits</a> 

我能想到的就是更改整个 href 值。是否有一种“合理”的方法可以在事件触发时仅更改 location= 的值。

最佳答案

为您的链接提供一个 ID

<a href="/search?location=&input=Suits" id="my_link">Suits</a> 

并使用js修改

var your_location = "Melbourne Australia"; // can be dynamic

var obj = document.getElementById('my_link');

var h = obj.href;

var string_to_find = "location=";
var string_to_replace = "location="+your_location;
var new_h = h.replace(string_to_find, string_to_replace);

obj.href=new_h;

关于javascript - 在 Javascript 事件上更改 URL(在 HTML 内)中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917147/

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