关于jquery 的on 方法
以前一直是用 低版本的jq 库 1.7 1.8左右的, 然后绑定事件都是用的 静态的用bind绑定,动态的用live 绑定的, 现开发新的项目打算用1.9 版的库 听说1.9 的on 已合并了 bind ,live,delegate 但是奇怪的是 我做了个测试还是没办法实现 live的效果啊, 动态生成的元素还是没办法成功绑定事件,
这是什么情况 ?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Scripts/jquery-1.9.1.min.js"></script>
</head>
<body>
<table>
<tr>
<td>
<p id="pclick">点我给页面添加一个按钮</p>
</td>
</tr>
</table>
<script>
$(function () {
$("#pclick").on("click", function () {
$("<input type='button' value='test' id='btnTest' />").insertAfter($(this));
});
$("#btnTest").on("click", function () {
alert("进来了");
});
})
</script>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<table>
<tr>
<td>
<p id="pclick">点我给页面添加一个按钮</p>
</td>
</tr>
</table>
<script>
$(function () {
$("#pclick").on("click", function () {
var newE=$("<input type='button' value='test' id='btnTest' />").bind('click',function () {
alert("进来了");
});
newE.insertAfter($(this));
});
})
</script>
</body>
</html>
$("<input type='button' value='test' id='btnTest' />").bind('click',function () {
alert("进来了");
}).insertAfter($(this));