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

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

问题描述

我最近将jQuery从1.8更新为2.1。我突然发现 .live()停止工作。

我得到错误 TypeError:$(...) .live不是函数



是否有任何方法可以用来替代 .live()

解决方案

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



这意味着如果您从版本1.8及更早版本升级,如果您不遵循下面的迁移指南,会注意到事情会中断。您不能简单地用 .on()!替换 .live()




在开始搜索并替换之前阅读:



对于活动网站上的 / hot fixes ,不要只需替换关键字 live 上,参数不同

  .live(事件,功能)

应该映射到:

  .on(eventType,selector,function)

(孩子)选择器非常重要!如果因任何原因不需要使用它,请将它设置为 null






迁移示例1:

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

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

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






迁移示例2:



之前:

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

之后,移动元素( .myButton .on()选择器,找到最近的父元素(最好带ID):
$ b ('click','.myButton',function)$ b pre $ $('#parentElement')。 $ p>

如果您不知道该怎么做作为父母, body 始终有效:

  $('body')。on('click','.myButton',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.

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

解决方案

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()!


Read before you start doing a search and replace:

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)

should map to:

.on(eventType, selector, function)

The (child) selector is very important! If you do not need to use this for any reason, set it to null.


Migration Example 1:

before:

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

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

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


Migration Example 2:

before:

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

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)

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

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


See also:

这篇关于jQuery 1.9 .live()不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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