jQuery 1.9 .live() 不是函数 [英] jQuery 1.9 .live() is not a function

查看:23
本文介绍了jQuery 1.9 .live() 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将 jQuery 从 1.8 更新到 2.1.我突然发现 .live() 停止工作.
我收到错误 TypeError: $(...).live is not a function.

I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live() stops working.
I get the error TypeError: $(...).live is not a function.

有什么方法可以代替 .live() 吗?

Is there any method I can use in place of .live()?

推荐答案

jQuery .live() 已从 1.9 版本开始移除.

这意味着如果您从 1.8 版及更早版本升级,如果您不遵循下面的迁移指南,您会发现事情很糟糕.您不能简单地将 .live() 替换为 .on()

jQuery .live() has been removed in version 1.9 onwards.

That means if you are upgrading from version 1.8 and earlier, you will notice things breaking if you do not follow the migration guide below. You must not simply replace .live() with .on()!

对于实时站点上的快速/热修复不要将关键字live替换为on,
由于参数不同

For quick/hot fixes on a live site, do not just replace the keyword live with on,
as the parameters are different!

.live(events, function)

应该映射到:

.on(eventType, selector, function)

(子)选择器很重要!如果出于任何原因不需要使用它,请将其设置为 null.

之前:

$('#mainmenu a').live('click', function)

之后,将子元素 (a) 移动到 .on() 选择器:

after, you move the child element (a) to the .on() selector:

$('#mainmenu').on('click', 'a', function)

<小时>

迁移示例 2:

之前:


Migration Example 2:

before:

$('.myButton').live('click', function)

之后,将元素 (.myButton) 移动到 .on() 选择器,并找到最近的父元素(最好带有 ID):

after, you move the element (.myButton) to the .on() selector, and find the nearest parent element (preferably with an ID):

$('#parentElement').on('click', '.myButton', function)

如果你不知道把什么作为父元素,body 总是有效的:

If you do not know what to put as the parent, body always works:

$('body').on('click', '.myButton', function)

<小时>

另见:

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