jQuery / Javascript替换< space>与%20的锚链接

问题描述:

我是jQuery的新手,我正在尝试编写一些代码来浏览页面并重写锚链接href属性,以便空格被删除并替换为%20。

I'm new to jQuery and i'm trying to write some code to go through the page and rewrite anchor links href attribute so that spaces are removed and replaced with %20.

到目前为止我有:

so far i have:

$(".row a").each(function(){
  $(this).attr("href").replace(/\s/g,"%20");
});

我试过几次这样的变化,没有运气。

I've tried a few variations of this with no luck.

您的方法是正确的,但您在替换它时忘记设置新值。试试这个:

Your approach is correct, but you're forgetting to set the new value once you replace it. Try this:

$(".row a").each( function() {
   this.href = this.href.replace(/\s/g,"%20");
});