帮助语法以便用另一个替换url

时间:2010-12-18 01:27:28

标签: jquery

使用jQuery,我试图自动替换每个...

www.mywebsite.com/blog/category/categoryname

通过

www.mywebsite.com/blog/#categoryname

...在我的页面中。

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

有人可以帮助解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为/category/是实际的,但categoryname会有所不同。如果这是正确的,请执行以下操作:

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

或类似的方法:

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

答案 2 :(得分:0)

我最终使用了以下

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

谢谢