简体   繁体   English

在Javascript中使用带有innerHTML的引号

[英]Using quotation marks in Javascript with innerHTML

I have written some code to update the player on the story as it progresses. 我已经写了一些代码来随着故事的进展更新播放器上的播放器。 When the player clicks a button they are greeted with some new text and some more options. 当玩家点击按钮时,他们会看到一些新文本和更多选项。 So far so good, but when I pass a function call in with a parameter attached, I need both single and double quote marks. 到目前为止一切都很好,但是当我使用附加参数传递函数调用时,我需要单引号和双引号。 However, if I use both then this breaks the innerHTML. 但是,如果我同时使用它们,则会打破innerHTML。 Code is as follows (can post HTML too if need be): 代码如下(如果需要,也可以发布HTML):

function buttonClicked(buttonid)  {
     switch(buttonid) {
    case "YesStart":
        document.getElementById("storybox").innerHTML = "Initially she fails to notice you and stares into the distance with 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><p>When you arrive at the hotel it 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 leave, but the room is always impeccably kept, so the uneasy feeling the hotel staff have about him has never yet been officially corroborated.</p>";
        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>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>";
        break;
    default:
       console.log("Do you write your button function call correctly? Bad function call. Bad.");
    };

};

Can anyone suggest a solution? 谁有人建议解决方案?

You have to escape them with \\. 你必须用\\来逃避它们。

"This is a string with \"quotation\" marks."

How to include quotes in a string 如何在字符串中包含引号

Choose one type of quote as the wrapper, eg " , then for attribute wrappers, use the other, eg ' . If you then need any other embedded quotes, escape them like so \\' . 选择一种类型的引用作为包装器,例如" ,然后对于属性包装器,使用另一种,例如' 。如果您需要任何其他嵌入式引号,请将它们转义为\\'

document.getElementById("storybox").innerHTML = "Initially she fails to notice you and stares into the distance with 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><p>When you arrive at the hotel it 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 leave, but the room is always impeccably kept, so the uneasy feeling the hotel staff have about him has never yet been officially corroborated.</p>";

For future reference: please make more concise examples. 供将来参考:请提供更简洁的示例。

Since this is HTML, I'd try using &quot; 由于这是HTML,我会尝试使用&quot; in the string. 在字符串中。

 function test1() { eval("console.log('\\'text\\'')"); } function test2() { eval("console.log(\\"'text'\\")"); } function test3() { eval("console.log('\\\\'text\\\\'')"); } 
 button { margin-bottom: .25em; } 
 <button onclick="console.log("'text'")">Wrong</button> <button onclick='console.log("'text'")'>Wrong too</button> <br> <button onclick="console.log(&quot;'text'&quot;)">Right</button> <button onclick='console.log("&#39;text&#39;")'>Right too</button> <button onclick="console.log(&quot;&#39;text&#39;&quot;)">And this one is right</button> <br> <button onclick="console.log('\\'text\\'')">Right with escaping</button> <br><br> <button onclick="test1()">Wrong in js</button> <br> <button onclick="test2()">Escaping outer in js</button> <button onclick="test3()">Escaping inner in js js</button> <br><br> <button onclick=console.log("'text'")>And the final one - this way works too</button> 

More information (in Russian): https://ru.stackoverflow.com/a/456039/178988 更多信息(俄语): https//ru.stackoverflow.com/a/456039/178988

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM