1、页面动态生成的dom元素,监听事件失效。需用事件代理进行监听。

对于动态绑定元素可以这样写

$(document).on('click', '.xxx', function() {
// do something
});

$(document)可以改成要绑定事件元素的父节点

.xxx 就是指的当前元素

这样就可以实现事件的代理

.on(events, callback) 只能绑定页面已有元素的事件。
.on(events, selector, callback) 则是在 已有的元素 上绑定 代理的 事件处理器 (addEventListener 实际上在该已有元素上调用),但只有事件的实际 source 是其子代元素并且符合 selector 时, callback 才会以该实际 source 为 this指向的对象被调用。

For example:

$(document).on("click", "a", function () {
console.log(this.tagName.toLowerCase()); // "a"
return false;
});

这样即可监听页面创建时尚未存在的 <a> 元素所产生的事件。

http://api.jquery.com/on/

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored.

渣翻译:

委托事件 的优势是可以处理在其后添加入文档的子代元素所产生的事件。通过选择一个添加委托事件时必然存在的元素,你可以使用委托事件以避免频繁添加和移除事件处理器的需求。例如,该元素可以是在 MVC 设计中视图的包含元素,或者如果事件处理器要监控文档中所有冒泡的事件时,则是 documentdocument 在载入任何其它 HTML 之前即可在文档的头部获得,所以在此添加事件处理器而不用等待 document 的 ready 是安全的。

除了处理尚未创建的子代元素上产生的事件,委托事件的另一个优势是需要监控多个元素时的性能更好。

jQuery 问题收集的更多相关文章

  1. Jquery插件收集

    移动端滚动条插件iScroll.js http://www.cnblogs.com/starof/p/5215845.html http://www.codeceo.com/article/35-jq ...

  2. jquery 资料收集

    1.jquery 特效:http://www.yeshou-jquery.com/yeshou/jquery.html 2.jquery 无缝文字滚动效果(跑马灯效果)插件 Marquee(MSCla ...

  3. 一些jquery特效收集

    jQuery幻灯片插件带投影的图片叠加切换幻灯片轮播 特效:http://www.jsfoot.com/jquery/images/ jquery文字滚动上下间歇文字滚动 http://www.17s ...

  4. Jquery插件收集【m了慢慢学】

    1. Simple Effects for Drop-Down Lists 一个jQuery插件用于将普通的select控件转成一个带有一些简单扩展效果的下拉列表. 2. X-editable 这个插 ...

  5. 工作中js和jquery 函数收集

    1. 判断单选框是否选中  $("xxx").is(":checked")       返回类型是 true/false 2. $(this)[0].nextS ...

  6. 【转】20个令人敬畏的jQuery插件

    为网页设计师和开发推荐20个令人敬畏的jQuery插件.例如滑块,图像画廊,幻灯片插件,jQuery的导航菜单,jQuery文件上传,图像旋转器,标签的插件,用户界面​​元素,网络接触形式,模态窗口, ...

  7. 左侧点击后右侧添加tab标签栏以及内容

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. ajax--实现异步请求,接受响应及执行回调

    ajax最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页的内容 ajax指是一种创建交互式网页应用的网页开发技术,其实就是实现前后端交互. 1)ajax是异步javascr ...

  9. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

随机推荐

  1. 微软手写识别模块sdk及delphi接口例子

    http://download.csdn.net/download/coolstar1204/2008061 微软手写识别模块sdk及delphi接口例子

  2. 工作笔记:phpstrom、docker、phpunit进行单元测试

  3. vuejs基础-跑马灯效果

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  4. Spring cloud学习--Zuul01

    Zuul解决的问题 作为系统的统一入口,屏蔽了系统内部各个微服务的细节 可以与微服务治理框架结合,实现自动化的服务实例维护以及负载均衡的路由转发 实现接口权限校验与微服务业务逻辑的解耦 搭建Zuul服 ...

  5. [Linux] 007 目录处理命令

    1. 目录处理命令:mkdir 命令名称:mkdir 命令英文原意:make directories 命令所在路径:/bin/mkdir 执行权限:所有用户 语法:mkdir -p [目录名] 功能描 ...

  6. web调试代理工具Whistle

    由于最近在学习微信小程序开发,项目中用到了https代理请求,所以用到了基于Node实现的跨平台web调试代理工具Whistle,在此做一记录. 完成https代理请求总共需要5个步骤. 一.安装No ...

  7. NGUI的窗体的推动和调节大小(drag object和drag resize object)

    一,我们先添加一个sprite,给sprite添加一个背景图片,然后attach添加一个box Collider,但是这时我们右键attach是找不到drag object的我们需要在add comp ...

  8. ajax axios 下载文件时如何获取进度条 process

    最近项目需要做一个下载文件的进度条,看网上上传文件进度条下载,特分享出来方便大家查阅 <!DOCTYPE html> <html> <head>     <m ...

  9. windows使用ubuntu启动linux服务

    有些服务只能在linux中策马奔腾,但是公司配置windows电脑,因此在windows中安装ubuntu服务,再在启动的ubuntu中启动linux服务 系统:win10(其他系统没试过) 安装步骤 ...

  10. golang-练习1

    题目: 输入字符串,返回最大的单词. 实例:run#¥@!time   返回:time package main import ( "fmt" "strings" ...