0

为什么我不能将动态文本放在名为 foo 的 div 上?以下是 asp.net 代码隐藏中的方法。

StringBuilder script = new StringBuilder();
        script.Append("<script type='text/javascript'>");
        script.Append("                                $(document).ready(function () {");
        script.Append("                                    $('<div id=foo></div>').appendTo('body');");
        script.Append("                                    var dlg = $('#foo');");
        script.Append("                                    dlg.dialog({");
        script.Append("                                         autoOpen: true,");
        script.Append("                                         resizable: false,");
        script.Append("                                         draggable: false,");
        script.Append("                                         modal: true,");
        script.Append("                                         title: 'Información',");
        script.Append("                                         width: 400,");
        script.Append("                                         height: 200,");
        script.Append("                                         buttons: {");
        script.Append("                                            'Cerrar': function () {");
        script.Append("                                            $(this).dialog('close');");
        script.Append("                                         }");
        script.Append("                                       }");
        script.Append("                                   });");
        script.Append("     $('#foo').html(<p>"+info+"</p>);");
        script.Append("                                });");

        script.Append("</script>");

        ClientScript.RegisterStartupScript(this.GetType(), "launchModalWindow", script.ToString());

问题出在这一行:

script.Append("     $('#foo').html(<p>"+info+"</p>);");
4

3 回答 3

5
script.Append("$('#foo').html('<p>hola</p>');");

在 document.ready 的上下文之外。

将其向上移动一行,以便在页面准备好加载时运行它。

StringBuilder script = new StringBuilder();
    script.Append("<script language='javascript'>");
    script.Append("                                $(document).ready(function () {");
    script.Append("                                    $('<div id=foo></div>').appendTo('body');");
    script.Append("                                    var dlg = $('#foo');");
    script.Append("                                    dlg.dialog({");
    script.Append("                                         autoOpen: true,");
    script.Append("                                         resizable: false,");
    script.Append("                                         draggable: false,");
    script.Append("                                         modal: true,");
    script.Append("                                         title: 'Información',");
    script.Append("                                         width: 400,");
    script.Append("                                         height: 200,");
    script.Append("                                         buttons: {");
    script.Append("                                            'Cerrar': function () {");
    script.Append("                                            $(this).dialog('close');");
    script.Append("                                         }");
    script.Append("                                       }");
    script.Append("                                   });");
    script.Append("$('#foo').html('<p>hola</p>');");
    script.Append("                                });");
    script.Append("</script>");
于 2012-07-09T12:48:23.223 回答
-1

像这样放置动态文本:

script.Append("                                    $('<div id=foo class=vw1>"+ info +"</div>').appendTo('body');");
于 2012-07-09T14:47:46.247 回答
-1

应该是<div id="foo">。您缺少引号。

此外,您希望在

$(document).ready(function() {
    $('#foo').html('<p>hola</p>');
}
于 2012-07-09T12:48:49.497 回答