javascript - 如何替换 src 域名 tld

标签 javascript jquery

我需要更改图像 src 中的 tld 和 div 的背景图像 如本例所示:

来自

<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=19393262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>

<div style="width: 100%; height: 100%; background-image: url(https://new.domain.org/image.aspx?id=19393262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>

来自

<img id="dynamic" class="expand" src="https://ap.domain.com/imgx.aspx?id=5742757&amp;type=newsHi">

<img id="dynamic" class="expand" src="https://new.domain.org/imgx.aspx?id=5742757&amp;type=newsHi">

最佳答案

使用此代码更改图像的 src。

Array.from(document.querySelectorAll('img')).forEach(x => {
  x.src = x.src.replace(/ap.domain.com/,'new.domain.org')
  console.log(x);
});

这将遍历所有图像标签并更改它们的 src。

要更改 div 的背景图像,请使用以下代码。

Array.from(document.querySelectorAll('div')).forEach(x => {
  x.style.backgroundImage = x.style.backgroundImage.replace(/ap.domain.com/,'new.domain.org')
  console.log(x);
});

Array.from(document.querySelectorAll('div')).forEach(x => {
  x.style.backgroundImage = x.style.backgroundImage.replace(/ap.domain.com/,'new.domain.org')
  console.log(x);
});

Array.from(document.querySelectorAll('img')).forEach(x => {
  x.src = x.src.replace(/ap.domain.com/,'new.domain.org')
  console.log(x);
});
<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=19393262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>
<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=19334262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>
<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=19234262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>
<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=19121262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>
<div style="width: 100%; height: 100%; background-image: url(https://ap.domain.com/image.aspx?id=11293262&amp;x=2&amp;y=1); background-size: cover; background-repeat: no-repeat; border-radius: 0px;"></div>
<img id="dynamic" class="expand" src="https://ap.domain.com/imgx.aspx?id=5337&amp;type=newsHi">

<img id="dynamic" class="expand" src="https://ap.domain.com/imgx.aspx?id=512&amp;type=newsHi">

<img id="dynamic" class="expand" src="https://ap.domain.com/imgx.aspx?id=57412757&amp;type=newsHi">

<img id="dynamic" class="expand" src="https://ap.domain.com/imgx.aspx?id=5741212757&amp;type=newsHi">

关于javascript - 如何替换 src 域名 tld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467231/

相关文章:

javascript - hashchange 上的 Jquery 调用函数

jQuery 事件处理程序总是按照它们被绑定(bind)的顺序执行——有什么办法解决这个问题吗?

javascript - 可编辑表格(单击时删除文本并添加文本字段)触发次数过多

javascript - 从 json 文件检索数据以创建 slider

javascript - 创建折叠评论区

javascript - 简单的 Jquery CheckAll/UncheckAll 复选框

javascript - 使用 if else 语句将谷歌地图添加到网页

javascript - 使用衰减正弦波从 Val 过渡到 Val

javascript - 使用 jQuery 函数切换 div

javascript - Twig 模板无法识别 Javascript