由于项目组做的人太多了,项目整体比较乱, jquery架包在项目中导入了3个,1.3.2, 1.5.2, 1.9.1 我的天.....
每次在修改别人代码的时候总是自动忽略引入的js和css ,所以导致今天上午纠结了一上午,才明白了,废话不说了, 正式进入话题...
开始喽。
一直在使用.live()觉得很好用,特别是在绑定事件之后再加入的元素的事件绑定上很方便(第一次live之后以后添加的元素就不需要绑定啦),甩bind()好几条街,但是由于没有仔细研究live(). 今天就在这里摔了个大跟头。
在jsp导入的jQuery.1.9.1,但是我在js中使用的是$("#liveID").live("click",function(){alert("liveclick");});
后来使用了神气提示错误信息为:
神气如下,灰常好用:
<scriptlanguage="javascript" type="text/javascript">
//错误代码测试
onerror=function(msg,url,line){
var errorMsg = msg+",\nurl"+url+",\n line:"+line;
alert(errorMsg);
}
</script>
后来查了文档才知道在新版本中做了修改。
jQuery网站上这么说的:
live在jQuery1.7中就不再建议使用,在1.9中就给删除了。在jQuery官方网站上说明了原因,并声称用on方法取代live方法。jQuery官方网站声称下面三行代码是等价的
Asof jQuery 1.7, the .live() method is deprecated.Use .on() to attach event handlers. Usersof older versions of jQuery should use .delegate() in preference to .live().
Thismethod provides a means to attach delegated event handlers to the document elementof a page, which simplifies the use of event handlers when content isdynamically added to a page. See the discussion of direct versus delegatedevents in the.on() method for more information.
改进后的使用建议:
1$(selector).live(events, data, handler); // jQuery 1.3+
2$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+
3$(document).on(events, selector, data, handler); // jQuery 1.7+
示例:
1 $("a.offsite").live("click", function(){alert("Goodbye!"); }); // jQuery 1.3+
2$(document).delegate("a.offsite", "click", function(){alert("Goodbye!"); }); // jQuery 1.4.3+
3$(document).on("click", "a.offsite", function(){alert("Goodbye!"); }); // jQuery 1.7+
结束喽!希望对你们有所帮助。