小编Rob*_*ujo的帖子

反正有没有减少这个JavaScript switch语句?

我正在使用这个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 …
Run Code Online (Sandbox Code Playgroud)

javascript switch-statement

2
推荐指数
1
解决办法
349
查看次数

标签 统计

javascript ×1

switch-statement ×1