我想在图像加载失败时更改与该图像相关的文本,并将该文本设置为红色,该文本已经可以工作,但穴居人风格,这就是为什么我在这里需要您的帮助document.getElementById("0").onerror = function() {Fu0()};document.getElementById("1").onerror = function() {Fu1()};document.getElementById("2").onerror = function() {Fu2()};function Fu0() { document.getElementById("0").style.display = "none"; var x = document.getElementById("T0"); x.innerHTML = "ERROR"; x.style.color = "red"; x.style.fontWeight = "bolder" x.style.backgroundColor="800000"; }; function Fu1() { document.getElementById("1").style.display = "none"; var x = document.getElementById("T1"); x.innerHTML = "ERROR"; x.style.color = "red"; x.style.fontWeight = "bolder" x.style.backgroundColor="800000"; };function Fu2() { document.getElementById("2").style.display = "none"; var x = document.getElementById("T2"); x.innerHTML = "ERROR"; x.style.color = "red"; x.style.fontWeight = "bolder" x.style.backgroundColor="800000"; };我在 HTML 上出现了 20 多次这样的情况,这意味着我在循环中考虑使用 (i) 运行大量的行,但我无法使其工作。我已经尝试过这个和一些变体,但没有成功。for (i = 0; i < 26; i++) {document.getElementById(i).onerror = function() {Fu()};}function Fu() { document.getElementById(i).style.display = "none"; var x = document.getElementById("T"+i); x.innerHTML = "ERROR"; x.style.color = "red"; x.style.fontWeight = "bolder" x.style.backgroundColor="800000"; };如果您能帮助我,我将非常感激,提前致谢
1 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
for (let i = 0; i < 26; i++) {
document.getElementById(i).onerror = function() {Fu(i)};
}
function Fu(num) {
document.getElementById(num).style.display = "none";
var x = document.getElementById("T"+ num);
x.innerHTML = "ERROR";
x.style.color = "red";
x.style.fontWeight = "bolder"
x.style.backgroundColor="800000";
};
- 1 回答
- 0 关注
- 54 浏览
添加回答
举报
0/150
提交
取消