This question already has an answer here:
这个问题已经有了答案:
- Turning live() into on() in jQuery 5 answers
- 在jQuery 5中将live()转换为on()
I used live()
for generated pages and frames. But in jQuery 1.9
this function is deprecated and does not work.
我对生成的页面和框架使用live()。但是在jQuery 1.9中,这个函数被弃用了,不能工作。
I use on()
instead of live()
but this method works for one time, and does not work in frames.
我使用on()而不是live(),但是这个方法只工作了一次,不能在帧中工作。
My code looks like this:
我的代码是这样的:
$("#element").live('click',function(){
$("#my").html(result);
});
What is the solution?
解决方案是什么?
1 个解决方案
#1
70
$('body').on('click', '#element', function(){
$("#my").html(result);
});
The clicked element selector is now passed through the .on()
function parameters, and the previous selector should be replaced with the closest parent selector preferably with an ID. If you do not know what parent selector to use, body
works too, but is less efficient.
单击的元素选择器现在通过.on()函数参数传递,前面的选择器应该用最近的父选择器替换,最好使用ID。
see jQuery 1.9 .live() is not a function on how to migrate existing code.
请参见jQuery 1.9 .live(),它不是一个关于如何迁移现有代码的函数。
#1
70
$('body').on('click', '#element', function(){
$("#my").html(result);
});
The clicked element selector is now passed through the .on()
function parameters, and the previous selector should be replaced with the closest parent selector preferably with an ID. If you do not know what parent selector to use, body
works too, but is less efficient.
单击的元素选择器现在通过.on()函数参数传递,前面的选择器应该用最近的父选择器替换,最好使用ID。
see jQuery 1.9 .live() is not a function on how to migrate existing code.
请参见jQuery 1.9 .live(),它不是一个关于如何迁移现有代码的函数。