开发者

Help with syntax in order to replace url by another

Using jQuery, I am trying to automatically replace every...

www.mywebsite.com/blog/category/categoryname

by

ww开发者_运维问答w.mywebsite.com/blog/#categoryname

...in my page.

$("a[href^='...']")
 .each(function()
 { 
  this.href = this.href.replace(...);
 }
);

Can someone help with the syntax please?


I assume /category/ is actual, but categoryname varies. If that's correct, do this:

this.href = this.href.replace( '/category/', '/#' );

or a similar approach:

this.href = this.href.split('/category/').join('/#');


$('a[href*=category]').attr('href', function(i,h){ return h.replace(/category\//,'#'); });


I ended up using the following

$(".cat-menu a[href*=category]")
        .each(function()
        { 
        this.href = this.href.replace( "/category/", "/#" );
        }
    );

Thank you

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜