以下是js替换src域名的示例:

假设你的网站的所有图片都来自于 `https://example.com`,但是你想要改为从 `https://cdn.example.com` 加载图片,可以使用以下代码:
js
document.querySelectorAll('img').forEach(function(img) {
img.src = img.src.replace('https://example.com', 'https://cdn.example.com');
});
代码解析:
首先,我们使用 `document.querySelectorAll('img')` 获取所有的图片元素,并使用 `forEach` 方法遍历每一个图片元素。
然后,我们使用 `replace` 方法将图片元素的 `src` 属性中的 `https://example.com` 替换为 `https://cdn.example.com`。
最后,我们将更新后的 `src` 属性重新赋值给图片元素。

查看详情

查看详情