imgタグのsrcの一部をreplaceする
![](http://nerdspec.com/wp/wp-content/uploads/catch_js.png)
1 |
<img src="{$root}img/xxx.jpg"> |
を
1 2 3 4 5 6 7 |
$(function () { $("img").each(function() { var replace = null; var replace = $(this).attr('src').replace(/\{\$root\}/g,'../'); $(this).attr('src',replace); }); }); |
すると
1 |
<img src="../img/xxx.jpg"> |
となる。
aタグのhrefなどにも応用できる。