使用live delegate on解决js后装html故障问题
今天写一个前端的东西。每学到更多的知识。几下就能写几行代码、代码行数十个、代码几个文件量……这是真的。一直以来研究的前端遇到的问题仍然在实践百度谷歌问答。
我今天遇到这样的问题:已经写js代码,正确ajax加载html不灵。
随着几组keyword,最后这组帮我找到了解答的办法:
javascript not work on ajax content
非常幸运,我来到了:http://stackoverflow.com/questions/10161938/jquery-function-not-working-on-ajax-loaded-content
有经验的人回答了:
When you initially created the .click() handler, that second button was not yet in the DOM, so jQuery could not attach an event handler to it. The .live() function will work for any element that is already
in the DOM and also any element that will be added in the future (e.g. from an ajax call).
就是说。live能够将事件绑定到已经载入或没有载入的DOM里。
将原来写的
jQuery('.ajaxCommentMiniList a').click(function(){
alert(123);
return false;
});
改成:
jQuery('.ajaxCommentMiniList a').live('click', function(){
alert(123);
return false;
});
就可以。
网上不少文章建议使用delegate而不是live,甚至还有on。
http://stackoverflow.com/questions/4579117/jquery-live-vs-delegate
This, however, seems to me to be much more explicit about what is actually happening. You don't realise from the live example that the events are actually being captured on document; with delegate,
it is clear that the event capturing happens on #containerElement. You can do the same thing with live, but the syntax becomes increasingly horrid.
Specifying a context for your events to be captured also improves performance. With the live example, every single click on the entire document has to be compared with the selector a.myClass to see if it matches. With delegate, that is only the elements within
#containerElement. This will obviously improve performance.
Finally, live requires that your browser looks for a.myClass whether or not it currently exists. delegate only looks for the elements when the events are triggered, giving a further performance advantage.
NB delegate uses live behind the scenes, so you can do anything with live that you can do with delegate. My answer deals with them as they are commonly used.
Note also that neither live nor delegate is the best way to do event delegation in modern jQuery. The new syntax (as of jQuery 1.7) is with the on function. The syntax is as follows:
$('#containerElement').on('click', 'a.myClass', function() { ... });
看来on是更好的解决方式。于是我最后的代码改为:
jQuery('.ajaxCommentMiniList').on('click', '.pagination a',function(){
ajaxCommentMiniList.clickAction(jQuery(this));
return false;
});
找个时间研究一下:live VS delegate VS on。我希望这帮助部分遇到了同样的问题,我的朋友。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
使用live delegate on解决js后装html故障问题的更多相关文章
- (转)先装VS后装IIS产生问题的解决办法
原文地址:http://www.cnblogs.com/mytechblog/articles/1897682.html 基于.net的web程序设计asp.net,我们就必须安装VS和IIS,但这二 ...
- 解决js跨域问题
如何解决js跨域问题 Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下 的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨 ...
- promise 的基本概念 和如何解决js中的异步编程问题 对 promis 的 then all ctch 的分析 和 await async 的理解
* promise承诺 * 解决js中异步编程的问题 * * 异步-同步 * 阻塞-无阻塞 * * 同步和异步的区别? 异步;同步 指的是被请求者 解析:被请求者(该事情的处理者)在处理完事情的时候的 ...
- 如何解决js跨域问题
Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨域访问,这就导致在一些a ...
- 学以致用:手把手教你撸一个工具库并打包发布,顺便解决JS浮点数计算精度问题
本文讲解的是怎么实现一个工具库并打包发布到npm给大家使用.本文实现的工具是一个分数计算器,大家考虑如下情况: \[ \sqrt{(((\frac{1}{3}+3.5)*\frac{2}{9}-\fr ...
- 爬虫:selenium + phantomjs 解决js抓取问题(一)
selenium模块主要用来做测试,模拟键盘.鼠标来操作浏览器. phantomjs 就像一个无界面的浏览器一样. 两个结合能很好的解决js抓取的问题. 测试代码: #coding=utf-8 fro ...
- 先装.net后装iis的问题
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:(即就是先装的是visual stuido 2010的话,在安装IIS 7) 32位的Windows:----------- ...
- bower解决js的依赖管理
bower解决js的依赖管理 前言: 一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如jQuery,去官网下载名为jquery-1.10.2.min.js文件,放到我们的项目 ...
- 先装Net Framework 后 装 IIS的处理办法
先装IIS话,后面装Net Framework时候会自动注册 处理aspx和ashx等的处理扩展程序 先装Net Framework 后 装 IIS.扩展程序注册在命令:aspnet_regiis - ...
随机推荐
- async和await用法
原文:async和await用法 要理解async和await的用法,首先要了解Task相关知识,这里不做说明,因为这不是本文的重点. 如果你已经对Task很了解,那么如何使用async和await, ...
- MYSQL中取当前年份的第一天和当前周,月,季度的第一天/最后一天
mysql 获取当年第一天的年月日格式:SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY); MySQL里获取当前week.month ...
- unity3d教程动态创建简单平面地形
unity3d创建地形是不须要usingUnityEditor的.这里使用了AssetDatabase.所以需using UnityEditor; 创建三步: 1.TerrainData terrai ...
- iOS 多线程开发之OperationQueue(二)NSOperation VS GCD
原创Blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...
- 使用AngularJS开发下一代Web应用
原版的:https://github.com/edagarli/AngularJSWeb 来源书:https://github.com/shyamseshadri/angularjs-book 版权声 ...
- ACM:回溯,八皇后问题,素数环
(一)八皇后问题 (1)回溯 #include <iostream> #include <string> #define MAXN 100 using namespace st ...
- jQuery回到顶部插件jQuery GoUp
插件描写叙述 jQuery GoUp!是一个简单的jQuery插件,让你的网页用户直接回到顶部. 用法很easy 引用jquery库和jquery.goup.min.js到你的页面 <scrip ...
- 基于最简单的FFmpeg采样读取内存读写:存储转
===================================================== 基于最简单的FFmpeg样品系列读写内存列表: 最简单的基于FFmpeg的内存读写的样例:内 ...
- Lua相关的知识
http://stackoverflow.com/questions/5438751/how-to-debug-lua-remotely http://cn.bing.com/search?q=org ...
- —页面布局实例———win7自己的小算盘
晚上7各地点布局,9点半,刚拿到. 他发现自己专注的时候效率挺高真的哈萨克斯坦.计算器布局前.做了两件简单的页面布局练练手.今晚总体感觉更好,不难. 器之间调试有点蛋疼,真心不想搭理IE. 在进行布局 ...