I'm looking to change the links in two containers I have from link.php to ../link.php on certain pages.
Can someone help my with the jQuery code I need to do this?
The containers are two divs, one with 开发者_开发问答the id="nav" and one with the id="fnav"
Thanks
I assume by links, you mean the a
elements. You can use the multiple selector, to get a reference to your divs, search for all a
elements using find
and change the href
with attr
:
$('#nav, #fnav')
.find('a')
.attr('href', function(i, value){return '../' + value});
You can pass a function to attr
which gets the old value as parameter. So this prepending all paths with "../"
.
If you only want to change the links that refer to index.php
, then use the attribute selector: .find('a[href="index.php"]')
Should be pretty simple:
$('#nav, #fnav').find('a').attr('href', function(index, href) {
return '../' + href;
});
Ref.: .attr()
精彩评论