gpt4 book ai didi

javascript - 有没有办法减少这个 JavaScript switch 语句?

转载 作者:行者123 更新时间:2023-12-03 17:12:21 24 4
gpt4 key购买 nike

我正在使用此 switch 语句在 404 页面上加载不同的图像和文本,它运行良好,但我只是想知道是否有更好的方法来编写它?简写吗?

<script>
var funFacts = Math.floor(Math.random() * 7) + 1;
switch (funFacts) {
case 1:
document.getElementById("funFactText").innerHTML = "In Florida, it is against the law to put livestock in a school bus.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff1.jpg")";
break;

case 2:
document.getElementById("funFactText").innerHTML = "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff2.jpg")";
break;

case 3:
document.getElementById("funFactText").innerHTML = "In Alaska, it is illegal to look at a moose from the window of an aircraft or another flying vehicle. It is also illegal to push a live moose out of a moving aircraft.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff3.jpg")";
break;

case 4:
document.getElementById("funFactText").innerHTML = "In Ohio, women are prohibited from wearing patent leather shoes in public.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff4.jpg")";
break;

case 5:
document.getElementById("funFactText").innerHTML = "By law, everybody in Vermont must take at least one bath a week.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff5.jpg")";
break;

case 6:
document.getElementById("funFactText").innerHTML = "In Illinois, the law is that a car must be driven with the steering wheel.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff6.jpg")";
break;

case 7:
document.getElementById("funFactText").innerHTML = "California law prohibits a woman from driving a car while dressed in a housecoat.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff7.jpg")";
break;
}
</script>

最佳答案

是的,使用数组作为事实,以及一些简单的字符串连接:

var funFacts = [
"In Florida, it is against the law to put livestock in a school bus.",
"In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.",
"In Alaska, it is illegal to look at a moose from the window of an aircraft or another flying vehicle. It is also illegal to push a live moose out of a moving aircraft.",
"In Ohio, women are prohibited from wearing patent leather shoes in public.",
"By law, everybody in Vermont must take at least one bath a week.",
"In Illinois, the law is that a car must be driven with the steering wheel.",
"California law prohibits a woman from driving a car while dressed in a housecoat."
];
var funFact = Math.floor(Math.random() * 7);
document.getElementById("funFactText").innerHTML = funFacts[funFact];
var baseSrc = "@Url.Content("~/themes/PG/Content/Images/ff")";
document.getElementById("funFactImg").src = baseSrc + (funFact + 1) + '.jpg';

关于javascript - 有没有办法减少这个 JavaScript switch 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31327628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com