[英].on() not working on ajax loaded page
My soft.php code 我的soft.php代码
<!DOCTYPE html>
<html>
<head><style>
</style>
<script src="jquery.js"></script>
<script>
$(function(){
$("button").on("click",function(){alert("f");});
$("div").on("click",function(){
$("span").load("check.php");
});
});</script></head><body>
<div>Click</div>
<button>eaf</button><span></span>
</body></html>
And check.php code 和check.php代码
<body>
<button>Hello</button>
</body>
I want to alert f on clicking on button which is loaded via ajax which was working fine with .live(). 我想通过点击按钮来提醒f,该按钮是通过ajax加载的,它与.live()工作正常。 But someone told me that .on() can be used in place of live but .on() is not working here.Suggest Solution
但是有人告诉我.on()可以用来代替live。但是.on()在这里不起作用。解决方案
$('body').on("click", "button", function(){
alert("f");
});
As your button
added to DOM after page load you need to use delegate event. 当您在页面加载后将
button
添加到DOM时,您需要使用委托事件。
It would be better to use something other selector instead of body
. 最好使用其他选择器代替
body
。
You can use multiple selector like follwoing: 你可以使用多个选择器,如下面的:
$('body').on("click", "button, div", function(){
alert("f");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.