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

我今天遇到这样的问题:已经写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. Android于fragment_main.xml文件问题组件收购

    package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...

  2. HDU 4126 Genghis Khan the Conqueror MST+树形dp

    题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...

  3. Xamarin for android:为button设置click事件的几种方法

    原文:Xamarin for android:为button设置click事件的几种方法 在Xamarin中一个最基础的事情,就是为一个button指定click事件处理方法,可是即使是这么一件事也有 ...

  4. linux下使用vi操作

    ESC : 进入命令模式 linux下使用vi后,怎样跳转到文件结尾 pagedown键连续按 虽然我也这么用,但还是太笨了.问了高手,方法是按shift+g,另外,到文件开头是gg.   linux ...

  5. (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

    题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. Windows Phone开发(4):框架和页

    原文:Windows Phone开发(4):框架和页 在开如之前,我想更正一个小问题,之前我在第一篇文章中说,Visual Studio Express for Windows Phone的中文版没有 ...

  7. 关于读style元素定义样式表兼容性

    <span style="font-size:18px;"></span><pre name="code" class=" ...

  8. 阿里云server该数据光盘安装操作

    猛击这里:阿里云server该数据光盘安装操作

  9. PL SQLDEVELOPMENT导出数据库脚本

    Tools--export Tables--选择表--SQL Inserts-- watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHprMTU2MjExMD ...

  10. ICTCLAS用的字Lucene4.9捆绑

    它一直喜欢的搜索方向,虽然无法做到.但仍保持了狂热的份额.记得那个夏天.这间实验室.这一群人,一切都随风而逝.踏上新征程.我以前没有自己.面对七三分技术的商业环境,我选择了沉淀.社会是一个大机器,我们 ...