jQuery - 如何使用“on()”方法而不是“live()”? [英] jQuery - how to use the "on()" method instead of "live()"?

查看:137
本文介绍了jQuery - 如何使用“on()”方法而不是“live()”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 live()来生成页面和框架。但是在 jQuery 1.9 这个函数已被弃用且不起作用。

I used live() for generated pages and frames. But in jQuery 1.9 this function is deprecated and does not work.

我使用 on ()而不是 live()但此方法有效一次,并且在帧中不起作用。

I use on() instead of live() but this method works for one time, and does not work in frames.

我的代码如下所示:

  $("#element").live('click',function(){
    $("#my").html(result);
   });

解决方案是什么?

推荐答案

$('body').on('click', '#element', function(){
    $("#my").html(result);
});

点击的元素选择器现在通过 .on()函数参数,前一个选择器应替换为最近的父选择器,最好是ID。如果您不知道要使用哪个父选择器, body 也可以,但效率较低。

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.

参见< a href =https://stackoverflow.com/q/14354040/584192> jQuery 1.9 .live()不是关于如何迁移现有代码的功能。

这篇关于jQuery - 如何使用“on()”方法而不是“live()”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆