2012-07-09 52 views
0

爲什麼我不能把動態文本放在一個名爲foo的div上?以下是關於asp.net代碼隱藏的一種方法。可以使用jquery設置di​​v的內容

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>);"); 
+1

沒有立即* *看到的問題。但請注意,不是'$('

').appendTo('body');'後面跟着'var dlg = $('#foo');',您可以簡單而直接地執行'var dlg = $('
').appendTo('body');' – 2012-07-09 12:47:33

+1

當該行被執行並且div'foo'尚不存在時,文檔可能沒有完全準備好?其實是的,這是答案。我沒有看到你正在'document.ready'上創建'foo',這將在這行執行之前。 – 2012-07-09 12:48:33

+1

請注意,'script'標記的'language'屬性已被棄用了十多年。相關屬性是['type'](http://www.w3.org/TR/html5/the-script-element.html#attr-script-type),但如果腳本是JavaScript,則不需要需要指定它,如果省略'type',則缺省值爲'「text/javascript」'。 – 2012-07-09 12:49:36

回答

-1

把動態文本這樣的:

script.Append("         $('<div id=foo class=vw1>"+ info +"</div>').appendTo('body');"); 
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>"); 
+0

謝謝!有效。但現在我有另一個問題。我需要在這個對話框中添加一個動態文本。我要更新線程。 – anmarti 2012-07-09 13:55:37

+0

但是我不知道爲什麼這條線必須在準備好的環境中。你能解釋我嗎? – anmarti 2012-07-09 14:00:37

+1

*「但是我不知道爲什麼這行必須在準備好的上下文中。」*因爲'ready'處理程序中的代碼在** ready代碼處理程序之外的代碼之後發生**,即使該代碼是更進一步的來源:http://jsbin.com/izubad – 2012-07-09 15:05:42

-1

應該是<div id="foo">。你缺少引號。

而且,你想要做你的更新#foo在

$(document).ready(function() { 
    $('#foo').html('<p>hola</p>'); 
} 
+0

*「您缺少引號。」*引號是[完全可選](http://www.w3.org/TR/html5/syntax.html#attributes-0),如果該值不包含空格, '','','','''','<', '>'或'''' – 2012-07-09 12:52:40

+0

謝謝,有時候我忘了你可以做壞html,它會允許它 – 2012-07-09 20:56:42

+0

@ Rocky:這不是「壞」。 (而且它總是被允許的HTML4規範大約:1999年,這是最早的任何一種嚴格的應用:http://www.w3.org/TR/html401/intro/sgmltut.html如果屬性值僅包含字母(a到z和A到Z),數字(0到9),連字符(ASCII小數點45)或句點(),則允許CDATA屬性不帶引號。 ASCII小數點46)。「* ref:http://www.w3.org/TR/REC-html32) – 2012-07-09 21:51:16

相關問題