今天做项目转移,jquery由1.8.0升级为1.9.1,在做一个新页面时,原程序的common.js中涉及到live方法失效,网上大家的解释如下,收藏并验证通过,内容如下:
原文地址:http://stackoverflow.com/questions/14354040/jquery-1-9-live-is-not-a-function
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 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)
本文介绍从 jQuery 1.8 到 1.9 的升级过程中,如何将原有的 live 方法转换为 on 方法,确保事件绑定的兼容性和正确性。
1312

被折叠的 条评论
为什么被折叠?



