我想替换基于查询字符串的链接的自定义id

I want to replace the custom id of links based on query string?

本文关键字:字符串 链接 id 自定义 查询 替换 于查询      更新时间:2023-09-26

我有以下html代码在我的网站(www.mysite.com):

<a href="www.website.com?rh=1234">Link 1</a>
<a href="www.website.com/page/?rh=1234">Lin… 2</a>

我想做的是,当有一个查询字符串"r"附加到我的网站的地址示例www.mysite.com?r=9876, 链接应该改变为:

<a href="www.website.com?r=9876">Link 1</a>
<a href="www.website.com/page/?r=9876">Link 2</a>

我想替换'rh=1234'与'r=query_string_id',它应该只发生在一个特定的域,www.website.com在这种情况下。我想这是可能的javascript,但我不确定如何做到这一点。我在研究中发现了这个,但它并不是我想要的。我想改变发生只有当有一个查询字符串在我的网站的url。可以有人请张贴javascript代码,使这成为可能。我也在使用博客平台。

$('a[href*=+'YOUR_WEBSITE'+]').each(function() {
    this.href = this.href.split('?rh=').join('?r=');
});

如果rh是第一个查询参数,

应该工作。
$('a[href*="website.com?rh"]').each(function()
{
    var curr_value = $(this).attr('href'); // you dont ectually need this
    var new_value = 'whatever your new links is';
    $(this).attr('href' , $new_value);
});