jQuery 错误“意外令牌非法”中的反斜杠

Backslashes in jQuery Error "Unexpected token ILLEGAL"

本文关键字:非法 错误 意外 令牌 jQuery      更新时间:2023-09-26

我只是无法让我的代码工作。我想使用 jQuery 将一个多行字符串附加到我的身体。但是当我为每个换行符使用反斜杠定义字符串时,我收到"意外令牌非法"错误。我做错了什么?我的代码:

    var first = true;
function showPopup(text, title) {
    //Fügt HTML und CSS für Popup-Box zum Body des HTML Dokumentes hinzu.
    if (first) {
        var string1 = '<div id="popup">'
        <div id="content">'
            <div id="title"></div>'
            <div id="text"></div>'
        </div>'
        <a href="javascript:togglePop()"><div id="popup_button"><i class="fa fa-times"></i> Schließen</div></a>'
        </div>'
    <div id="popoverlay"></div>';

下面的行是第一个错误:

        var string2 = '<style>'
    #popup{'
    width: 50%;'
    transform: translate(-50%, -50%);'
    position: fixed;'
    left: 50%;'
    top:   50%;'
    background-color: white;'
    color: black;'
    z-index: 1000;'
    box-shadow: 2px 2px 10px black;'
    display: none;'
}'
#popup #content{'
    padding: 20px;'
}'
#popup #title{'
    font-weight: bold;'
    font-size: 110%;'
}'
#popup_button{'
    text-align: center;'
    padding: 10px 0px;'
    color: #790300;'
    cursor: pointer;'
    transition-duration: 0.25s;'
}'
#popup_button:hover{'
    transition-duration: 0.25s;'
    background-color: #EDEDED;'
}'
#popup a{'
    text-decoration: none;'
}'
#popoverlay{'
    display: none;'
    background-color: black;'
    position: fixed;'
    top: 0%;'
    left: 0%;'
    width: 100%;'
    height: 100%;'
    z-index: 100;'
    opacity: 0.7'
}'
</style>';
        $("body").append(string1);
        $("body").append(string2);

        first = false;
    }
    //Setzt den Titel und den Text der Popup-Box.
    $("#popup #text").innerHTML = text;
    $("#popup #title").innerHTML = title;
    //Öffnet die Box
    togglePop();
}

那么,我做错了什么?我将不胜感激。

空行也必须转义:

a = 'Hello'
World!' # WRONG!

a = 'Hello'
'
World!' # Correct.