我們在開發過程會遇到無法給動態元素添加綁定事件,解決方案如下:
例如
需要給<ul>里面動態添加的<li>標簽添加click事件
1.jquery 1.7版以前使用live動態綁定事件
2.jquery 1.7版以后使用on動態綁定事件
$(
"#testdiv ul"
).on(
"click"
,
"li"
,
function
() {
//do something here
});
$(
"#testdiv ul"
).attr("click","方法名");
希望本文所述對大家的jQuery程序設計有所幫助。