jquery无法为动态生成的元素添加点击事件

如果您的元素是使用点击事件填充的,您的操作必须包含在此点击事件中。

示例:

$(.clickMeToAppendElement").click(function(){

$(.toBeAppend").append("这是元素内容");$("""""")classtree").on("click",function(){

alert("终于进入了");

});

);if如果你运行如果放置在单击事件之外,则在页面初始加载时找不到.J_classtree类。
无法添加此功能。

/p>

<html>

<head>

<title>Javascript中的点击事件方法1</title>

</head>

<body>

点击</button>

[xss_clean]

varbtn=document.getElementById("btn");

btn.onclick=function(){

alert("helloworld");

}

[xss_clean]

</body>

</html>

方法2-

<!DOCTYPEhtml>

<html>

<head>

<title>单击Javascript中的两个事件方法</title>

</head>

<body>

点击</button>

[xss_clean]

varbtn=document.getElementById("btn");

btn.addEventListener('点击',有趣action(){

alert("hellowrold");

},false)

[xss_clean]

</body>

</html>

jqueryhtml动态添加的元素绑定事件详解

在实际开发中,会遇到需要给动态生成的html元素绑定触发事件的情况: