事件代理日常总结
2016-01-04 12:24
393 查看
js是单线程的,添加事件的时候如果正好加载异步数据,dom节点很有可能没有加载上就已经进入了事件队列,导致dom无法添加事件
以上是我用的事件代理,基本原理就是在父类元素添加事件,这样父类可以在加载前就进入事件队列,可以添加事件,添加事件后自行处理吧。。
同时使用事件代理不用为所有的子类添加事件,这样在有大量的子类时可以减少添加事件的消耗。
var s = document.querySelector('body'); var tem; s.addEventListener('click', function (e) { if (e.target.tagName.toLowerCase() === 'button' && e.target.className.indexOf('cancel-order') >= 0) { $('#cancel-reason').val(''); $('#cancel-content').val(''); tem = $(e.target); var info = tem.parent().parent().find('.orderId').text(); $('#info-order').text(info); $('.mask').show(); $('.jump-layer').show(); } });
以上是我用的事件代理,基本原理就是在父类元素添加事件,这样父类可以在加载前就进入事件队列,可以添加事件,添加事件后自行处理吧。。
同时使用事件代理不用为所有的子类添加事件,这样在有大量的子类时可以减少添加事件的消耗。
/** * @file Describe the file */ var olTpl = __inline('./selectSpot.tmpl'); module.exports = { init: function () { var self = this; self.$content = $('#content-list'); $.get('main/gettouristplace', {city_id:2912}, function (data) { var data = JSON.parse(data); self.$content.append(olTpl(data)); console.log($('input[type=checkbox]:checked').length); console.log($('label .check2').length); }); var s = document.querySelector('body'); var tem; s.addEventListener('click', function (e) { if (e.target.tagName.toLowerCase() === 'i' && e.target.className.indexOf('check2') >= 0 && e.target.className.indexOf('iconfont') >= 0) { console.log(166); console.log($('input[type=checkbox]:checked').length); console.log($('input[type=checkbox]:checked')); if ($('input[type=checkbox]:checked').length === 10) { console.log(3435); $('input[type=checkbox]').not("input:checked").attr('disabled', 'true'); console.log($('input[type=checkbox]').not("input:checked").length); } } }); } };
相关文章推荐
- [转]浅谈缓冲区溢出之栈溢出
- MySQL 支持GeoDjango的简单说明
- cocos纯代码写一个简单的菜单
- ros 安装 urdf 包
- gdi+中Drawpath()返回值为OutOfMemory的问题
- C語言 rand函数 进阶探讨与实现
- jQuery TextEx 简介信息
- linux文件系统命令和分区 挂载
- poj1573&&hdu1035 Robot Motion(模拟)
- 使用 Android Studio 更换app应用图标
- pop 背景的变暗
- Socket编程实践(3) --Socket API
- MOGODB REDIS
- Socket编程实践(2) --Socket编程导引
- R语言与数据分析之九:时间内序列--HoltWinters指数平滑法
- 微信公众平台修改服务器配置时token验证失败
- 中文分词器分词效果的评测方法
- Unity3D中使用Profiler精确定位性能热点的优化技巧
- linux 命令 —— mkdir
- Socket编程实践(1) --TCP/IP简述