求助,动态加载js问题?在线等... 动态加载div 后,发现js事件失效,想绑定一个点击事件,只要点击就重新加载这段js代码,应该怎么写呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 之前给div绑定过click事件,但是动态加载div后,click事件不执行 为什么不执行???你动态加载div是否绑定了这个事件,查看下源文件是否有猫腻!!! 重新加载的div中怎么赋值event?建议加载完毕后dom赋值event 这个是以前做的动态添加 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(""); } <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>我这儿没问题! javascript里如何进行简单的图形处理? 可以通过js生成8位数的数字随机码吗? 梅花雪树改变某个节点的图片 请教关于javascript路径 setAttribute()不能给元素指派名为name的属性吗? 如何让一个多行文本框的边框不显示? jqGrid 分页总页数不显示 javascript的转义符是\吗?为什么\'无效呢? 请问怎么用JS一个像素一个像素地画图? echarts 异步切换数据显示问题 QQ留言功能该如何实现! JQuery如何接收web方法返回的日期值?
为什么不执行???
你动态加载div是否绑定了这个事件,查看下源文件是否有猫腻!!!
建议加载完毕后dom赋值event
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("");
}
<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>我这儿没问题!