JQuery HTML不终止字符串文本错误如果HTML包含空格

本文关键字:HTML 如果 包含 空格 错误 字符串 终止 JQuery 文本 | 更新日期: 2023-09-27 18:09:05

通过将此代码设置为元素的innerHtml的一部分,将其插入到页面的HTML中。它创建了一个类似于按钮的span,当点击这个span时,会打开一个JQueryUI窗口,其中包含一些恒定的文本,并在该窗口下方显示文本"Thiswindow"。

string custom_training_html = "Thiswindow"; // Note this line here.
link = "<span class='"start_course_button'" onclick=";
link += "javascript:$('"#temp_test_window'").dialog({minWidth:800,minHeight:600});$('"#temp_test_custom_html'").html('" + custom_training_html + "');>Test JQueryUI";
link += "</span>'n";

然而,如果我把文本"Thiswindow"改为"Thiswindow",我得到:

SyntaxError: unterminated string literal

HTML中任何地方的空格都会破坏代码。这个网站上的其他问题说问题是HTML中的换行符,但我想我没有。我只是建立一个包含<span>代码的字符串,工作版本和非工作版本之间的唯一区别是一个包含空格字符。怎么了?

供参考,被打开的窗口看起来像这样:

<div id="temp_test_window" title="Chaucer's Prologue (The Monk)">
    A monk there was, one made for mastery<br />... [snip]
    <div id="temp_test_custom_html">Content Goes Here</div>
</div>

EDIT:我已经清理了代码,将JS放入自己的函数中,但这并没有解决问题。在我的主。aspx文件中,我有:

function GenerateTestTrainingContent(content)
            {
                $('#temp_test_window').dialog({minWidth:800,minHeight:600});
                $('#temp_test_custom_html').html(content);
            }
[and later, outside the <script> tags:]
<div id="temp_test_custom_html">Content Goes Here</div>

在。aspx.cs文件中,我有以下代码创建一些HTML作为"link":

link = "<span class='start_course_button' onclick=javascript:GenerateTestTrainingContent('OneWord');>";
link += "Test JQueryUI</span>'n";

工作,给我一个链接,调用一个弹出式JQueryUI窗口,其中显示文本"OneWord"。但是,如果我在相同的代码中将'OneWord'更改为'OneWord',我得到:

SyntaxError: unterminated string literal
javascript:GenerateTestTrainingContent('One

同样,没有空格=有效,没有空格=有效

JQuery HTML不终止字符串文本错误如果HTML包含空格

正如我在评论中提到的,我会将JS移动到它自己的<script>块。我还将清理"'的使用,以使事情不那么混乱。

string custom_training_html = "This window"; // Note this line here.
string link = "";
link += "<span class='start_course_button'>Test JQueryUI</span>'r'n";
link += "<script>'r'n";
link += "$('#temp_test_window').dialog({ autoOpen: false, minWidth:800, minHeight:600 });'r'n";
link += "$('#temp_test_custom_html').html('" + custom_training_html + "');'r'n";
link += "$('.start_course_button').click(function(){ $('#temp_test_window').dialog('open'); });'r'n";
link += "</script>'r'n";