How to update (append to) an href in jquery?

后端 未结 4 556
悲&欢浪女
悲&欢浪女 2020-11-27 15:19

I have a list of links that all go to a google maps api.

the links already have the daddr (destination) parameter in them as static. I am using Geo-Loca

相关标签:
4条回答
  • 2020-11-27 16:00
    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');
    });
    
    0 讨论(0)
  • 2020-11-27 16:13
    $("a.directions-link").attr("href", $("a.directions-link").attr("href")+"...your additions...");
    
    0 讨论(0)
  • 2020-11-27 16:15

    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

    0 讨论(0)
  • 2020-11-27 16:23

    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

    0 讨论(0)
提交回复
热议问题