用javascript创建后HTML消失了

时间:2010-02-18 11:06:34

标签: asp.net javascript xml

解决这个问题后,我遇到了另一个问题

Trying to read XML file but he is always the same

我正在动态创建HTML元素,并提供从XML文件中读取的值,元素总是消失但是在创建之后。有什么理由发生这种情况?我的代码是在cs文件中

script = "function OnClientDragEnd(dock, args)" +
                            "{" +                    
                            "   req = false; " +
                            "   var isIE = false;" +
                    // branch for native XMLHttpRequest object
                            "   if(window.XMLHttpRequest && !(window.ActiveXObject)) {" +
                            "       try {" +
                            "           req = new XMLHttpRequest();" +
                            "       } catch(e) {" +
                            "           req = false;" +
                            "       }" +
                    // branch for IE/Windows ActiveX version
                            "   } else if(window.ActiveXObject) {" +
                            "       try {" +
                            "           req = new ActiveXObject('Msxml2.XMLHTTP');" +
                            "       } catch(e) {" +
                            "           try {" +
                            "               req = new ActiveXObject('Microsoft.XMLHTTP');" +
                            "           } catch(e) {" +
                            "               req = false;" +
                            "           }" +
                            "       }" +
                            "   }" +
                            "   if(req) {" +
                            "       req.onreadystatechange = function(){processReqChange(dock,args)};" +
                            "       req.open('GET', 'Config.xml', false);" +
                            "       req.send('');" +
                            "   }" +
                            "}" +
                            "function processReqChange(dock,args) {" +
                                // only if req shows "loaded"
                            "   if (req.readyState == 4) {" +
                                    // only if "OK"
                            "       if (req.status == 200) {" +
                                    // ...processing statements go here...
                            "           var contagemNos = req.responseXML.documentElement;" +
                            "                           var txt = contagemNos.childNodes(i).getElementsByTagName('Titulo')[0].text;" +//alert(txt);
                    "                           var ta = contagemNos.childNodes(i).getElementsByTagName('Id')[0].previousSibling; var tatext = ta.text;" +//alert(tatext);
                    "                           var ni = document.getElementById('spanObjDock');" +
                    "                           var divIdName = 'myDiv';" +
                    "                           var newdiv = document.createElement('div');" +
                    "                           newdiv.setAttribute('id',divIdName);" +
                    "                           var labelTitulo = document.createElement('label');" +
                    "                           labelTitulo.id = 'span1';" +
                    "                           labelTitulo.innerHTML = 'Titulo';" +
                    "                           newdiv.appendChild(labelTitulo);" +
                    "                           var break1 = document.createElement('br');" +
                    "                           newdiv.appendChild(break1);" +
                    "                           var tboxTitulo = document.createElement('input');" +
                    "                           tboxTitulo.setAttribute('type', 'text');" +
                    "                           tboxTitulo.setAttribute('value', txt);" +
                    "                           tboxTitulo.setAttribute('name', 'tboxTitulo');" +
                    "                           tboxTitulo.setAttribute('id', 'tboxTitulo');" +
                    "                           if (tboxTitulo.addEventListener){" +
                    "                               var enviar = 'tboxTitulo';" +
                    "                               tboxTitulo.addEventListener('keyup', function(){updateValueTitulo(enviar);}, false);" +
                    "                           } else if (tboxTitulo.attachEvent){ " +
                    "                               var enviar = 'tboxTitulo';" +
                    "                               tboxTitulo.attachEvent('onkeyup', function(){updateValueTitulo(enviar);});" +
                    "                           }" +
                    "                           newdiv.appendChild(tboxTitulo);" +
                    "                           var break1 = document.createElement('br');" +
                    "                           newdiv.appendChild(break1);" +
                    "                           ni.appendChild(newdiv);" +   
                            "       } else {" +
                            "           alert('There was a problem retrieving the XML data: ' + req.statusText);" +
                            "       }" +
                            "   }" +
                            "}";
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "PositionChanged", script, true);

这是我在aspx文件中的代码

....
<asp:UpdatePanel runat="server" id="UpdatePanel3">
        <ContentTemplate>                
            <div id="spanObjDock"></div>
        </ContentTemplate>        
</asp:UpdatePanel>
....

1 个答案:

答案 0 :(得分:-1)

不完全是答案,但有两条建议:

  1. 使用jQuery(或类似的库,但MS支持jQuery,你得到Intellisense)。您粘贴的大部分代码都将通过jQuery解决,您不必担心不同的浏览器,您只需编写重要的代码(您的应用程序逻辑)。
  2. 使用Firefox + Firebug调试代码。您可以逐步添加断点并运行代码,以发现您对HTML的更改将在何时恢复。
  3. 链接:

    jQuery的: http://jquery.com/

    萤火虫: http://getfirebug.com/

    在Visual Studio 2008中使用jQuery: http://weblogs.asp.net/scottgu/archive/2008/11/21/jquery-intellisense-in-vs-2008.aspx

    jQuery教程: http://docs.jquery.com/Tutorials

    jQuery .get()方法: http://api.jquery.com/jQuery.get/

相关问题