if an a link contain something in the href change the href to this
i want to check if the href contains 'coming-soon' if it does change the href to go to my products page:
$('a[href$="coming-soon"]').attr('href', '/products.aspx');
cant work this out开发者_开发技巧.
$=
is "attribute-ends-with", use *=
for "attribute contains", like this:
$('a[href*="coming-soon"]').attr('href', '/products.aspx');
Use the contains selector.
$(document).ready(function(){
$("a[href*='coming-soon']").attr('href', '/products.aspx');
});
Try it here!
$=
means "ends with". You want to find links that contain (anywhere in the URL) coming-soon? Try *=
instead of $=
.
Your selector is currently any anchor tag whose href
ends with coming-soon
. The contains selector is *=
:
$('a[href*="coming-soon"]').attr('href', '/products.aspx');
See http://api.jquery.com/attribute-contains-selector/
If this isn't the problem, ensure the code is ran when the DOM is loaded; by wrapping the code in the $(document).ready()
$(document).ready(function () {
$('a[href*="coming-soon"]').attr('href', '/products.aspx');
});
精彩评论