使用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 - ...
随机推荐
- ORACLE函数之日期时间运算函数
1 ADD_MONTHS 格式:ADD_MONTHS(D,N) 说明:返回日期时间D加N月后相应的日期时间.N为正时则表示D之后:N为负时则表示为D之前.N为小数则会自己主动先删 ...
- js中escape的用法----前端页面简单加密
escape() 方法,它用于转义不能用明文正确发送的任何字符.比如,电话号码中的空格将被转换成字符 %20,从而能够在 URL 中传递这些字符. http://localhost:8080/a?na ...
- 转载:PHP时间戳 strtotime()使用方法和技巧
在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了 获取指定的年月日转化为时间戳:pHP时间戳函数获取指定日期的unix时间戳 strt ...
- Java乔晓松-android的四大组件之一Service(服务的绑定)
android的四大组件之一Service(服务的绑定) 怎么绑定服务,又怎么解除服务,代码如下: MainActivity.java源码: package com.example.lesson14_ ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Codeforces Round #191 (Div. 2) D. Block Tower
D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- C语言中的static 具体分析
google了近三页的关于C语言中static的内容,发现可用的信息非常少,要么长篇大论不知所云要么在关键之处几个字略过,对于想挖掘底层原理的刚開始学习的人来说參考性不是非常大.所以,我这篇博文博採众 ...
- C# 通过豆瓣网络编程API获取图书信息
这篇文章主要是关于如何通过豆瓣API获取信息的书籍,起初,我看到了原来的想法的内容是"C# 网络编程之网页简单下载实现"中通过HttpWebResponse类下载源代码,再通过正則 ...
- UVa 11879 - Multiple of 17
称号:计算一个数字是不17倍数. 说明:串,睑板. 简单的问题,直接推论可以是. 设定 n = 10a + d:(0 ≤ d ≤ 9) a - 5d = 51a - 5n,假设n被17整除,这个数必定 ...
- Sqlserver中Over函数
Over函数不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用. 其参数:over(partition by columnname1 order ...