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

时间:2011-10-25 09:14:38

标签: javascript jquery html string

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

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

我想要做的是,当我的网站的地址示例www.mysite.com?r=9876附有查询字符串“r”时,链接应该将更改为:

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

我想用'r = query_string_id'替换'rh = 1234',它应该仅在特定域上发生,在这种情况下是www.website.com。我想这可以用javascript,但我不知道该怎么做。我在研究期间发现了this,但这并不是我正在寻找的。我希望只有在我网站的网址中有查询字符串时才会发生更改。有人可以发布一个JavaScript代码,以实现这一目标。我也在使用博客平台。

2 个答案:

答案 0 :(得分:2)

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

如果rh是第一个查询参数,则应该有效。

答案 1 :(得分:0)

$('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);
});