在网址末尾添加一个字符串

时间:2014-12-19 10:21:38

标签: javascript jquery html

我在网站上有各种链接,并希望在每个网址末尾添加一个字符串,该网址在网址内有一个域example.com。例如:所有带域example.com的url都会在末尾“mystring”处有一个字符串,因此完整的URL将是:example.com?mystring。我试图完成这个,所以我可以在网址的末尾生成keybuilder字符串。

示例html结构:JsFiddle

这可能吗?感谢

4 个答案:

答案 0 :(得分:1)

您可以尝试以下

$(function(){
  //iterate all a tag having href="example.com"
  $('ul li a[href="example.com"]').each(function(){
    //set href with new value
    $(this).attr('href',$(this).attr('href')+'?myString');
  });
});

<强> DEMO

答案 1 :(得分:0)

FIDDLE

$('ul li a').click(function(){
   var domain=$(this).attr('href');
   alert(domain+'?myString');
});

答案 2 :(得分:0)

使用以下代码:

$('a[href^="example.com"]').each(function(){
    var href = $(this).attr('href');
    $(this).attr('href', href + '?myString');
});

答案 3 :(得分:0)

您可以使用attribute contains selector获取包含example.com的所有主播,然后只需设置href

$('a[href*="example.com"]').prop('href', function() {
   return this.href + '?yourString'; 
});