【发布时间】:2021-05-28 17:21:29
【问题描述】:
所以我正在解决这个问题,为 img src 属性附加前缀
例如
<img alt='' src='/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>
to
<img alt='' src='**myprefix**/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>
问题是这样的数组格式的字符串
[
"<img alt='' src='/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"
"<img alt='' src='/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"
"<img alt='' src='images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"
"<img alt='' src='/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"
"<img alt='' src='/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"
]
现在我有一个小的工作示例来获取图像 src,但不确定如何在数组中添加 src 属性前缀
var string ="<img alt='' src='http://api.com/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>";
var elem= document.createElement("div");
elem.innerHTML = string;
var images = elem.getElementsByTagName("img");
for(var i=0; i < images.length; i++){
console.log(images[i].src);
}
我是 JavaScript 的绝对初学者,因此任何指导都会有所帮助。提前致谢
【问题讨论】:
-
前缀 src atttr 是什么意思?!
-
对上述数组中的错字表示歉意,我需要将“api.com”附加到已经存在的 src="/images/UID" 中,这样我就有 src="api.com/images/UID"
标签: javascript html arrays