jQuery 1.9 .live() is not a function
jQuery 1.9 .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
.
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:
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-08-05 HearthBuddy BotManager
2019-08-05 HearthBuddy版本收集
2019-08-05 Could not attach to a Hearthstone process.
2019-08-05 C# 7 .NET / CLR / Visual Studio version requirements
2019-08-05 Difference between C# compiler version and language version
2019-08-05 HearthBuddy CSharpCodeProvider 如何编译cs文件 升级编译器
2019-08-05 HearthBuddy DONOTDELETE.bin