[英]How to use JavaScript or jQuery to trim dynamic URL from absolute to relative
The system I'm using occasionally adds the full path to the src of a dynamic img. 我正在使用的系统偶尔会添加动态img的src的完整路径。
<a href="#" class="link">
<img src="http://www.domain.com/images/wish-add.png" alt="" />
</a>
I need everything before /images to to be removed. 我需要之前的一切/图像来删除。
I need be able to do it without manually referencing the domain. 我需要能够在不手动引用域的情况下执行此操作。 The domain is not always going to be the same.
域名并不总是一样的。
I can remove parts that are always the same.. Like /images.. but I don't know how to remove anything before /images regardless of what it is. 我可以删除总是相同的部分..喜欢/图像..但我不知道如何在/图像之前删除任何内容,无论它是什么。
$('img').attr('src', function (i, src) {
return src.replace(/https?:\/\/[^\/]+/, '');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.