i have various links on the site and would like to add a certain string at the end of each url that has a domain example.com inside the url. For example: all url's with domain example.com would have a string at the end "mystring", so the complete url would be: example.com?mystring
. I'm trying to acomplish this so i can generate keybuilder string at the end of urls.
Sample html structure: JsFiddle
Is this possible? Thanks
You can try like below
$(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');
});
});
$('ul li a').click(function(){
var domain=$(this).attr('href');
alert(domain+'?myString');
});
$('a[href^="example.com"]').each(function(){
var href = $(this).attr('href');
$(this).attr('href', href + '?myString');
});
You can use the attribute contains selector to get all anchors which contain example.com
, then just set the href
:
$('a[href*="example.com"]').prop('href', function() {
return this.href + '?yourString';
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.