如何在 jquery 中更新(附加) href?

我有一个链接列表,所有到谷歌地图应用程序接口。

链接中已经有 daddr(目标)参数作为静态参数。我使用地理定位找到用户的位置,我想添加的 saddr(源地址)的链接,一旦我得到的数据。

所以基本上我需要添加一些像 &saddr=50.1234567,-50.03452在所有的链接末端指向谷歌地图

所有的链接都有一个称为 directions-link的类

这一页我已经知道如何改变它们:

$("a.directions-link").attr("href", "http://www.google.com/");

然而,我只想将我的值附加到 href 的末尾,而不改变 href 已经是什么。

我该怎么做?

130745 次浏览
var _href = $("a.directions-link").attr("href");
$("a.directions-link").attr("href", _href + '&saddr=50.1234567,-50.03452');

To loop with each()

$("a.directions-link").each(function() {
var $this = $(this);
var _href = $this.attr("href");
$this.attr("href", _href + '&saddr=50.1234567,-50.03452');
});
$("a.directions-link").attr("href", $("a.directions-link").attr("href")+"...your additions...");

jQuery 1.4 has a new feature for doing this, and it rules. I've forgotten what it's called, but you use it like this:

$("a.directions-link").attr("href", function(i, href) {
return href + '?q=testing';
});

That loops over all the elements too, so no need for $.each

Here is what i tried to do to add parameter in the url which contain the specific character in the url.

jQuery('a[href*="google.com"]').attr('href', function(i,href) {
//jquery date addition
var requiredDate = new Date();
var numberOfDaysToAdd = 60;
requiredDate.setDate(requiredDate.getDate() + numberOfDaysToAdd);
//var convertedDate  = requiredDate.format('d-M-Y');
//var newDate = datepicker.formatDate('yy/mm/dd', requiredDate );
//console.log(requiredDate);


var month   = requiredDate.getMonth()+1;
var day     = requiredDate.getDate();


var output = requiredDate.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day;
//

Working Example Click