开发者

Help with syntax in order to replace url by another

开发者 https://www.devze.com 2023-01-31 08:50 出处:网络
Using jQuery, I am trying to automatically replace every... www.mywebsite.com/blog/category/categoryname

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

精彩评论

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