开发者

Changing the href of a link dynamiclly to only its hash value

开发者 https://www.devze.com 2023-02-28 17:03 出处:网络
I have a main menu on top of my site, its on every page, it has some links to coda slider panels (featurepage#开发者_开发问答1, featurepage#2 etc) which work to take the user directly to that panel, w

I have a main menu on top of my site, its on every page, it has some links to coda slider panels (featurepage#开发者_开发问答1, featurepage#2 etc) which work to take the user directly to that panel, when they are coming from a different page.

When Im actually on the FeaturePage, the links dont work, they only change the hash, without jumping to the relevant content.

As far as I can see, its because the links have the whole URL in them, rather than the needed #1, #2 etc.

So my question (and hope) is there a way of changing the HREF for those links just on that page, so featurepage#1 is turned into #1 and so on.

The problem as well its that its dynamic, I have no idea how many Hash values will be needed at any time, so I cant just simply replace them.

Would anyone have any ideas? Thanks!!!!


Well, this will remove everything before the # if one exists. I would think though that this would be better done from the backend. Don't have the page name in there to begin with.

$(function(){
    $('a').each(function(){
        var href=$(this).attr("href");
        if(href.indexOf("#")>-1){
            $(this).attr("href", href.substring(href.indexOf("#")))  
        }
    }); 
})
0

精彩评论

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

关注公众号