动态加载div 后,发现js事件失效,
想绑定一个点击事件,只要点击就重新加载这段js代码,应该怎么写呢?

解决方案 »

  1.   

    之前给div绑定过click事件,但是动态加载div后,click事件不执行
      

  2.   


    为什么不执行???
    你动态加载div是否绑定了这个事件,查看下源文件是否有猫腻!!!
      

  3.   

    重新加载的div中怎么赋值event?
    建议加载完毕后dom赋值event
      

  4.   

    这个是以前做的动态添加 div 以实现批量添加数据的js代码,你看看就清楚了function AddUpload() {
                        index++;
                        var template = $("#divTemplate").clone();
                        template.find("img[id=DelUpload]").attr("id", "DelUpload" + index);
                        template.find("select[id=ddlParent]").attr("id", "ddlParent" + index);
                        template.find("input[id=txtName]").attr("id", "txtName" + index);
                        template.find("textarea[id=txtRe]").attr("id", "txtRe" + index);
                        template.attr("id", "divTemplate" + index);
                        template.css("margin-top","10px");
                        template.find("div[id=divNameErrorMsg]").hide();
                        template.find("div[id=divKindErrorMsg]").hide();
                        
                        $("#MainDiv").append(template);
                        
                        $("#DelUpload"+index).unbind('click');
                        $("#DelUpload"+index).bind("click","#divTemplate"+index,RemoveUpload);
                        $("#txtName" + index).val("");
                        $("#txtRe" + index).val("");
                    }
      

  5.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function AddElement() {
                var oDiv = document.createElement("div");
                oDiv.style.background = "#c0c0c0";
                oDiv.style.width = "100px";
                oDiv.style.height = "100px";
                oDiv.appendChild(document.createTextNode("点击我看看"));
                oDiv.onclick = function () {
                    alert("我是动态添加上的元素");
                }
                document.body.appendChild(oDiv);
            }
        </script>
    </head>
    <body>
        <div>
            <input type="button" id="btClick" value="添加DIV" onclick="AddElement()" />
        </div>
    </body>
    </html>我这儿没问题!