是否有任何方法可以减少此JavaScript开关语句?

是否有任何方法可以减少此JavaScript开关语句?,javascript,switch-statement,Javascript,Switch Statement,我用这个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 la

我用这个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=Math.floor(Math.random()*7)+1;
开关(funFacts){
案例1:
document.getElementById(“funFactText”).innerHTML=“在佛罗里达州,将牲畜放在校车上是违法的。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff1.jpg”)”;
打破
案例2:
document.getElementById(“funFactText”).innerHTML=“在德克萨斯州,任何人拥有一把钳子都是违法的。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff2.jpg”)”;
打破
案例3:
document.getElementById(“funFactText”).innerHTML=“在阿拉斯加,从飞机或其他飞行器的窗口看麋鹿是违法的。将活麋鹿推出移动的飞机也是违法的。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff3.jpg”)”;
打破
案例4:
document.getElementById(“funFactText”).innerHTML=“在俄亥俄州,禁止女性在公共场合穿专利皮鞋。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff4.jpg”)”;
打破
案例5:
document.getElementById(“funFactText”).innerHTML=“根据法律,佛蒙特州的每个人每周必须至少洗一次澡。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff5.jpg”)”;
打破
案例6:
document.getElementById(“funFactText”).innerHTML=“在伊利诺伊州,法律规定汽车必须使用方向盘行驶。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff6.jpg”)”;
打破
案例7:
document.getElementById(“funFactText”).innerHTML=“加利福尼亚州法律禁止女性穿着家常服开车。”;
document.getElementById(“funFactImg”).src=“@Url.Content(“~/themes/PG/Content/Images/ff7.jpg”)”;
打破
}

可能看起来更干净,可以放入阵列,也可以从阵列中拉出

var funFacts = [
{
    text: "In Florida, it is against the law to put livestock in a school bus.",
    image: "~/themes/PG/Content/Images/ff1.jpg"
},
{
    text: "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.",
    image: "~/themes/PG/Content/Images/ff2.jpg"
},
...
];
var factIndex = Math.floor(Math.random() * funFacts.length); // can be combined with line below
var fact = funFacts[factIndex];
document.getElementById("funFactText").innerHTML = fact.text;
document.getElementById("funFactImg").src = "@Url.Content(\"" + fact.image + "\")";

是的,使用事实数组和一些简单的字符串连接:

var funFacts=[
“在佛罗里达州,把牲畜放在校车上是违法的。”,
“在德克萨斯州,任何人携带钳子都是违法的。”,
“在阿拉斯加,从飞机或其他飞行器的窗口看麋鹿是违法的。将活麋鹿推出移动的飞机也是违法的。”,
“俄亥俄州禁止女性在公共场合穿专利皮鞋。”,
“根据法律,佛蒙特州的每个人每周必须至少洗一次澡。”,
“在伊利诺伊州,法律规定汽车必须用方向盘驾驶。”,
“加利福尼亚州法律禁止妇女穿着家常服开车。”
];
var funFact=Math.floor(Math.random()*7);
document.getElementById(“funFactText”).innerHTML=funFacts[funFacts];
var baseSrc=“@Url.Content”(~/themes/PG/Content/Images/ff”);

document.getElementById(“funFactImg”).src=baseSrc+(funFact+1)+'.jpg'创建一个如下所示的对象数组:

{text:“某些事实…”,url:“某些url”}

让我们调用这个数组
facts

然后:


您可以完全取消开关,将数据存储在数组中,并根据选定的索引分配值:

var textElement = document.getElementById("funFactText");
var imgElement = document.getElementById("funFactImg");

var funFacts = Math.floor(Math.random() * 7);
var entries = [ 
    {
        text: "In Florida, it is against the law to put livestock in a school bus.",
        image: "@Url.Content("~/themes/PG/Content/Images/ff1.jpg")"
    },
    {
        text: "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.",
        image: "@Url.Content("~/themes/PG/Content/Images/ff2.jpg")"
    },
    {
        text: "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.",
        image: "@Url.Content("~/themes/PG/Content/Images/ff3.jpg")"
    },
    {
        text: "In Ohio, women are prohibited from wearing patent leather shoes in public.",
        image: "@Url.Content('~/themes/PG/Content/Images/ff4.jpg')"
    },
    {
        text: "By law, everybody in Vermont must take at least one bath a week.",
        image: "@Url.Content("~/themes/PG/Content/Images/ff5.jpg")"
    },
    {
        text: "In Illinois, the law is that a car must be driven with the steering wheel.",
        image: "@Url.Content('~/themes/PG/Content/Images/ff6.jpg')"
    },
    {
        text: "California law prohibits a woman from driving a car while dressed in a housecoat.",
        image: "@Url.Content("~/themes/PG/Content/Images/ff7.jpg")"
    }
];

textElement.innerHTML = entries[funFacts - 1].text;
imgElement.src = entries[funFacts - 1].image;

</script>
var textElement=document.getElementById(“funFactText”);
var imgElement=document.getElementById(“funFactImg”);
var funFacts=Math.floor(Math.random()*7);
var项目=[
{
正文:“在佛罗里达州,把牲畜放在校车上是违法的。”,
图片:“@Url.Content(“~/themes/PG/Content/Images/ff1.jpg”)”
},
{
文本:“在德克萨斯州,任何人持有钳子都是违法的。”,
图片:“@Url.Content(“~/themes/PG/Content/Images/ff2.jpg”)”
},
{
文本:“在阿拉斯加,从飞机或其他飞行器的窗口看麋鹿是违法的。将活麋鹿推出移动的飞机也是违法的。”,
图片:“@Url.Content(“~/themes/PG/Content/Images/ff3.jpg”)”
},
{
正文:“俄亥俄州禁止女性在公共场合穿专利皮鞋。”,
图片:“@Url.Content('~/themes/PG/Content/Images/ff4.jpg')”
},
{
正文:“根据法律,佛蒙特州的每个人每周必须至少洗一次澡。”,
图片:“@Url.Content(“~/themes/PG/Content/Images/ff5.jpg”)”
},
{
文本:“在伊利诺伊州,法律规定汽车必须使用方向盘行驶。”,
图片:“@Url.Content('~/themes/PG/Content/Images/ff6.jpg')”
},
{
正文:“加利福尼亚州法律禁止妇女穿着家常服开车。”,
图片:“@Url.Content(“~/themes/PG/Content/Images/ff7.jpg”)”
}
];
textElement.innerHTML=条目[funFacts-1].text;
imgElement.src=条目[funFacts-1].image;

只需注意重复的代码,并尝试将它们压缩到工作流中,然后检查与switch语句相关的变量发生了哪些变化。我不会说它是简写的,而是更少的代码

<script>
     var funFacts = Math.floor(Math.random() * 7) + 1;
     MyFunction(funFacts);

    function MyFunction(fun)
    {
       var text="";
       var link="";
        switch (fun) {
        case 1:
            text = "In Florida, it is against the law to put livestock in a school bus.";
           link = "~/themes/PG/Content/Images/ff1.jpg";
            break;
        case 2:
            text = "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.";
            link = "~/themes/PG/Content/Images/ff2.jpg";
            break;
        case 3:
            text = "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.";
            link = "~/themes/PG/Content/Images/ff3.jpg";
            break;
        case 4:
            text = "In Ohio, women are prohibited from wearing patent leather shoes in public.";
            link = "~/themes/PG/Content/Images/ff4.jpg";
            break;
        case 5:
            text = "By law, everybody in Vermont must take at least one bath a week.";
            link = "~/themes/PG/Content/Images/ff5.jpg";
            break;
        case 6:
            text = "In Illinois, the law is that a car must be driven with the steering wheel.";
            link = "~/themes/PG/Content/Images/ff6.jpg";
            break;
        case 7:
            text = "California law prohibits a woman from driving a car while dressed in a housecoat.";
            link = "~/themes/PG/Content/Images/ff7.jpg";
            break;
        }
        document.getElementById("funFactText").innerHTML = text;
        document.getElementById("funFactImg").src = "@Url.Content(link);
    }
    </script>

var funFacts=Math.floor(Math.random()*7)+1;
MyFunction(funFacts);
功能我的功能(乐趣)
{
var text=“”;
var link=“”;
开关(乐趣){
案例1:
text=“在佛罗里达州,将牲畜放在校车上是违法的。”;
link=“~/themes/PG/Content/Images/ff1.jpg”;
打破
案例2:
text=“在得克萨斯州,任何人拥有p都是违法的
<script>
     var funFacts = Math.floor(Math.random() * 7) + 1;
     MyFunction(funFacts);

    function MyFunction(fun)
    {
       var text="";
       var link="";
        switch (fun) {
        case 1:
            text = "In Florida, it is against the law to put livestock in a school bus.";
           link = "~/themes/PG/Content/Images/ff1.jpg";
            break;
        case 2:
            text = "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.";
            link = "~/themes/PG/Content/Images/ff2.jpg";
            break;
        case 3:
            text = "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.";
            link = "~/themes/PG/Content/Images/ff3.jpg";
            break;
        case 4:
            text = "In Ohio, women are prohibited from wearing patent leather shoes in public.";
            link = "~/themes/PG/Content/Images/ff4.jpg";
            break;
        case 5:
            text = "By law, everybody in Vermont must take at least one bath a week.";
            link = "~/themes/PG/Content/Images/ff5.jpg";
            break;
        case 6:
            text = "In Illinois, the law is that a car must be driven with the steering wheel.";
            link = "~/themes/PG/Content/Images/ff6.jpg";
            break;
        case 7:
            text = "California law prohibits a woman from driving a car while dressed in a housecoat.";
            link = "~/themes/PG/Content/Images/ff7.jpg";
            break;
        }
        document.getElementById("funFactText").innerHTML = text;
        document.getElementById("funFactImg").src = "@Url.Content(link);
    }
    </script>