开发者

How to change link path in certain container with jQuery

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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜