JavaScript switch语句中的语法错误

时间:2015-11-17 02:05:12

标签: javascript html switch-statement syntax-error

编码在线RPG,我在case statements为故事路径创建了Javascript。但是,当我运行代码时,我得到以下错误:第26行的"Uncaught SyntaxError: Unexpected token ILLEGAL",这是case语句之后的行:case "HotelStay":。我看不出代码有什么问题,但是当我把它和它下面的所有case语句都拿出来时(default:除外),代码运行正常。有什么想法吗?

function onStart()  {
     document.getElementById("storybox").innerHTML = "<p><em>You are Drogon Barre, aka Dragon-Bear.</em></p><p>The date is October 3rd. You are sitting quietly at your father's house when a letter arrives through the door with details of a rape and murder. The letter says that the murder was committed by a member of the infamous cult <strong>The Order of the Mouse</strong>. The writer claims the victim was her sister and that she looked on powerless as the assailant took her sister's life. She claims the perpetrator currently resides in a hotel just outside Plymouth. The letter gives a time and an address but no further details.</p><p>Do you choose to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"YesStart\")'>Yes</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>No</button>";
};

function buttonClicked(buttonid)  {
  switch(buttonid) {
    case "YesStart":
        document.getElementById("storybox").innerHTML = "You take the train down to Plymouth and meet at the location specificied. Initially the woman fails to notice you and stares into the distance with the dazed look of someone who has considered the exact point at which the universe might have ended, present within a kind of altered dimensionality that places her materially at a similar point of existence to you, while leaving her utterly absent from it in some other, more absolute sense. Two nuns scurry past her, heading towards a small, fluffy dog, who they pet while making cooing sounds; a stern man in an ill-fitting police officer's uniform eyes them suspiciously. This spectacle seems to rouse her from her trance, and she looks quizzically at them, tilting her head to one side, before spotting you out of the corner her eye and waving you over.</p><button type='button' class='btn btn-success' id='continuebtn' onclick='buttonClicked(\"ContinueStart\")'>Continue</button>";
        break;
    case "NoStart":
        document.getElementById("storybox").innerHTML = "<p>You try to fall asleep in your bed but you cannot. Your heart is beating so fast you feel like it will fail. All night you are plagued by images of your still conscious body being cut open at autopsy. You wake up often. During the days you are afraid to be left alone. The only solace you have is a lady who calls herself Deer-Wolf, who texts you often and calls you on occasion. You find her voice comforting but you are still afraid, and feel that you will never be fully safe until you solve the case. You ask your friends for help, even offer to pay them to investigate but they either ignore you, decline, or tell you to go away. You are not sure if they believe you and are too scared, or if they think you are crazy. Either way, they won't help.</p><p>Are you sure you don't want to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"NoStart\")'>No</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"YesStart\")'>Yes</button>";
        break;
    case "ContinueStart":
        document.getElementById("storybox").innerHTML = "<p>She takes you to a hotel. You chat a little on the way and she says her name is Deer-Wolf, which is the name of the lady you have been texting recently. You ask her about it and she tells you that yes, it was her who was sending the text messages. She is acting very unusually, speaking only when directly addressed and seemingly either angry with you or mired in a depression that makes your presence irrelevent. You suspect something of the two. When you arrive the hotel is empty but for two bleary eyed reception staff who stare as you walk past. Deer-Wolf tells you this is the place where the murderer lives. He rents a different room each week, always under different assumed names. He tells people this is because he is married, and likes to take women back unnoticed. The staff have never seen one of these women leave, but the room is always impeccably kept, and the uneasy feeling the hotel staff have about him has never yet been officially corroborated.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "HotelStart":
        document.getElementById("storybox").innerHTML = "<p>You investigate the hotel, but Deer-Wolf won't tell you the room number and you think you see the faint glimmer of a smile when you are about it. She tells you that you should talk to some of her friends who run a nudist group on the internet. They live in Swindon and she has invited you up so see them. You suspect she is playing a game with you. You could stay in the hotel on more night, or go investigate the group in Swindon Deer-Wolf suggests. Where will you go?.</p><button type='button' class='btn btn-success' id='mapbtn' onclick='buttonClicked(\"Map\")'>Go to Map</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelStay\")'>Stay in Hotel</button>";
        break;
    case "HotelHelp":
        document.getElementById("storybox").innerHTML = "<p>The only help you have at the moment is Deer-Wolf.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "Map":
        document.getElementById("storybox").innerHTML = "<p>Map Loads Here</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "HotelStay":
        document.getElementById("storybox").innerHTML = "<p>You stay overnight in the hotel. Deer-Wolf knocks at your door in the night. You go to open it and find her naked. She invites herself into your room.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"Invite\")'>Invite her in</button><button type='button' class='btn btn-warning' id='kissdeerbtn' onclick='buttonClicked(\"KissDeer\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that doesn't mean you
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "Invite":
        document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "Ask":
        document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    default:
       console.log("That part of the story hasn't been written yet.");
    };

};

onStart();

4 个答案:

答案 0 :(得分:3)

你应该在kissDeer条件的末尾加一个\,见下面的代码。

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you\
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;

答案 1 :(得分:1)

此行存在问题

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";

可以从新行开始。

用他的字符串加入

喜欢这个

document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";

<强> JSFIDDLE

答案 2 :(得分:1)

试试这个:你的绳子没有关闭在亲鹿案件上,我只是把它分成两部分。

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you"
        + "can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;

答案 3 :(得分:0)

只需将代码复制并粘贴到sublime就可以了。您在KissDeer案例中有新的换行符。

  

可以做你想做的事。

将它们放在一行中,代码就固定了。

case "KissDeer":
    document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
    break;