开发者

Using jQuery, help modifying a set of urls

开发者 https://www.devze.com 2023-01-25 09:32 出处:网络
I am getting a selection of URLs using: $(\"a.someclass\") Now my urls look like: http://www....com/something/12321

I am getting a selection of URLs using:

$("a.someclass")

Now my urls look like:

http://www....com/something/12321

I need to modify the urls by removing the trailing num开发者_开发问答ber to become:

http://www....com/something/

How can I do this?


Something like this might work, using a regexp to remove the trailing number:

$("a.someclass").each(function() {
  $(this).attr('href', function(index, attr) {
    return attr.replace(/^(.*)\d+$/, '$1');
  });
});


You could do this if the href actually ends with a number:

$("a.someclass").attr('href',function(i,href){return href.replace(/\d+$/,'');});
0

精彩评论

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

关注公众号