今天写一个前端的东西。每学到更多的知识。几下就能写几行代码、代码行数十个、代码几个文件量……这是真的。一直以来研究的前端遇到的问题仍然在实践百度谷歌问答。

我今天遇到这样的问题:已经写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故障问题的更多相关文章

  1. (转)先装VS后装IIS产生问题的解决办法

    原文地址:http://www.cnblogs.com/mytechblog/articles/1897682.html 基于.net的web程序设计asp.net,我们就必须安装VS和IIS,但这二 ...

  2. 解决js跨域问题

    如何解决js跨域问题 Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下 的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨 ...

  3. promise 的基本概念 和如何解决js中的异步编程问题 对 promis 的 then all ctch 的分析 和 await async 的理解

    * promise承诺 * 解决js中异步编程的问题 * * 异步-同步 * 阻塞-无阻塞 * * 同步和异步的区别? 异步;同步 指的是被请求者 解析:被请求者(该事情的处理者)在处理完事情的时候的 ...

  4. 如何解决js跨域问题

    Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨域访问,这就导致在一些a ...

  5. 学以致用:手把手教你撸一个工具库并打包发布,顺便解决JS浮点数计算精度问题

    本文讲解的是怎么实现一个工具库并打包发布到npm给大家使用.本文实现的工具是一个分数计算器,大家考虑如下情况: \[ \sqrt{(((\frac{1}{3}+3.5)*\frac{2}{9}-\fr ...

  6. 爬虫:selenium + phantomjs 解决js抓取问题(一)

    selenium模块主要用来做测试,模拟键盘.鼠标来操作浏览器. phantomjs 就像一个无界面的浏览器一样. 两个结合能很好的解决js抓取的问题. 测试代码: #coding=utf-8 fro ...

  7. 先装.net后装iis的问题

    如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:(即就是先装的是visual stuido 2010的话,在安装IIS 7) 32位的Windows:----------- ...

  8. bower解决js的依赖管理

    bower解决js的依赖管理 前言: 一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如jQuery,去官网下载名为jquery-1.10.2.min.js文件,放到我们的项目 ...

  9. 先装Net Framework 后 装 IIS的处理办法

    先装IIS话,后面装Net Framework时候会自动注册 处理aspx和ashx等的处理扩展程序 先装Net Framework 后 装 IIS.扩展程序注册在命令:aspnet_regiis - ...

随机推荐

  1. iPhone 6 首发无大陆,DevStore要去香港吗?

    iPhone 6 正式公布,微博已经被刷屏.iPhone 6 的各种信息都已经明了,先不说什么配置,什么设计,就说一点--iPhone6 首发地方是没有中国大陆的.这让我想起来最近參加的源代码大赛,这 ...

  2. auto property synthesis will not synthesize proterty ;it will be implementedby its superclass, use @

    Auto property synthesis will not synthesize property 'title'; it will be implemented by its supercla ...

  3. php截取等长UFT8中英文混合字串

    由于需要,想实现“php截取等长UFT8中英文混合字串”,可是网上找了很多代码不是有乱码就是不能实现等长(以一个中文长度为单位,两个英文字母算一个长度,如‘等长’长度为2,‘UTF8’长度也是2). ...

  4. java去全半角空格,trim(), replaceAll(" +",""),replaceAll("\\s*", ""), replaceAll(" | ", "")

    JAVA中去掉空格 . String.trim() trim()是去掉首尾空格 .str.replace(" ", ""); 去掉所有空格,包括首尾.中间 St ...

  5. hadoop学习;自己定义Input/OutputFormat;类引用mapreduce.mapper;三种模式

    hadoop切割与读取输入文件的方式被定义在InputFormat接口的一个实现中.TextInputFormat是默认的实现,当你想要一次获取一行内容作为输入数据时又没有确定的键.从TextInpu ...

  6. Qt数据类型转换

    把QString转换为double类型 方法1.QString str="123.45"; double val=str.toDouble(); //val=123.45 方法2. ...

  7. 苹果公司的新的编程语言 Swift 高级语言(十一)--初始化类的析构函数的一个实例

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程. 初始化包含设置一个实例的每个存储属性为一个初始值,以及运行不论什么其他新的实例可以使用之前须要的设置或 ...

  8. 【Android先进】查看手机记忆库状态和应用方法

    一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...

  9. Java二叉排序树(转)

    一.二叉排序树定义 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树,或者是满足如下性 ...

  10. 制作简易计算器处理过程Servlet

    CalculationServlet.java: package com.you.servlet; import java.io.IOException; import java.io.PrintWr ...