这里是新的。我无法访问网站上的大部分源文件,因此我尝试使用 JavaScript 修复页面加载时的一些损坏的图像。
当我对这些损坏的图像之一使用检查元素时,它显示如下:
<img src="-82.jpg" width="60px">
什么时候应该
<img src="http://example.com/files/images/-82.jpg" width="60px">
这适用于一堆不同的图像,-82.jpg、-2482.jpg、-3582.jpg
这是我到目前为止所尝试过的。这似乎可行,但由于某种原因它破坏了页面上的其他 JavaScript。
html
<script type="text/javascript" src="http://example.com/files/js/fiximages.js"></script>
<body onload="fixImages();">
我的 fiximages.js 文件
function fixImages() {
var toReplace = '<img src="-';
var replaceWith ='<img src="http://www.example.com/files/images/-';
document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}
我有点菜鸟,所以我还需要知道如何将 html 链接到 javascript,以便在页面加载时加载它。谢谢大家。
最佳答案
这应该可以解决您的问题:
function fixImages() {
// Create a list of all img which src starts with "-".
var imgs = document.querySelectorAll('img[src^="-"]');
// Loop through this list.
for (var i = 0; i < imgs.length; i++) {
// For each img, replace its src with the correct path + the
// src that's already there.
imgs[i].setAttribute('src', 'http://www.example.com/files/images/' + imgs[i].getAttribute('src'));
}
}
关于javascript - 相关图像损坏,需要 javascript 修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654469/