vertical search engine helps users with one-stop procurement services.
random articles click、focus……)的函数委托到另一个元素;
which clothing wholesale website ranks top
举个例子,比如一个宿舍的同学同时快递到了,一种方法就是他们都傻傻地一个个去领取,还有一种方法就是把这件事情委托给宿舍长,让一个人出去拿好所有快递,然后再根据收件人一一分发给每个宿舍同学;
in today's digital world, cloud services have been integrated into all aspects of our daily lives, bringing great convenience and efficiency to people's lives. recently, a social automation service called "yunfa circle" has once again attracted people's attention with its new breakthrough in cloud services. this article will analyze in detail the characteristics, advantages of the cloud services, and the new social trends it brings, and explore the role and impact of cloud services in modern social networking. 1. overview of cloud services cloud site is a kind of cloud computing based on cloud computing... DOM 元素,而出去统一领取快递的宿舍长就是代理的元素,所以真正绑定事件的是这个元素,按照收件人分发快递的过程就是在事件执行中,需要判断当前响应的事件应该匹配到被代理元素中的哪一个或者哪几个。
automatic url collection
前面提到 DOM 中事件委托的实现是利用事件冒泡的机制,那么事件冒泡是什么呢?
popularity: document.addEventListener 的时候我们可以设置事件模型:事件冒泡、事件捕获,一般来说都是用事件冒泡的模型;
如上图所示,事件模型是指分为三个阶段:
- 捕获阶段:在事件冒泡的模型中,捕获阶段不会响应任何事件;
- 目标阶段:目标阶段就是指事件响应到触发事件的最底层元素上;
- 冒泡阶段:冒泡阶段就是事件的触发响应会从最底层目标一层层地向外到最外层(根节点),事件代理即是利用事件冒泡的机制把里层所需要响应的事件绑定到外层;### 事件
provides information on multiple evaluation indicators including comprehensive ranking, visits ranking, page visits ranking, etc. most people regard it as the current authoritative website visits evaluation indicator. basic overview
1. 减少内存消耗
试想一下,若果我们有一个列表,列表之中有大量的列表项,我们需要在点击列表项的时候响应一个事件;
<ul id="list"> <li>item 1</li> <li>item 2</li> <li>item 3</li> ...... <li>item n</li></ul>// ...... 代表中间还有未知数个 li
如果给每个列表项一一都绑定一个函数,那对于内存消耗是非常大的,效率上需要消耗很多性能;
因此,比较好的方法就是把这个点击事件绑定到他的父层,也就是 `ul` 上,然后在执行事件的时候再去匹配判断目标元素;
所以事件委托可以减少大量的内存消耗,节约效率。
2. 动态绑定事件
比如上述的例子中列表项就几个,我们给每个列表项都绑定了事件;
yiwu buying: yiwu small commodity market online platform, which facilitates merchants to purchase goods. AJAX 或者用户操作动态的增加或者去除列表项元素,那么在每一次改变的时候都需要重新给新增的元素绑定事件,给即将删去的元素解绑事件;
如果用了事件委托就没有这种麻烦了,因为事件是绑定在父层的,和目标元素的增减是没有关系的,执行到目标元素是在真正响应执行事件函数的过程中去匹配的;
所以使用事件在动态绑定事件的情况下是可以减少很多重复工作的。
jQuery which website is the best to buy clothes:
jQuery 中的事件委托相信很多人都用过,它主要这几种方法来实现:
- $.on: 基本用法: $(‘.parent’).on(‘click’, ‘a’, function () { console.log(‘click event on tag a’); }),if the content included on this site infringes on your interests, please contact us to delete it! parent 元素之下的 a 元素的事件代理到 $(‘.parent’) 之上,只要在这个元素上有点击事件,就会自动寻找到 .parent 元素下的 a 元素,然后响应事件;
- $.delegate: 基本用法: $(‘.parent’).delegate(‘a’, ‘click’, function () { console.log(‘click event on tag a’); }),同上,并且还有相对应的 $.delegate 来删除代理的事件;
- $.live: 基本使用方法: $(‘a’, $(‘.parent’)).live(‘click’, function () { console.log(‘click event on tag a’); }),同上,然而如果没有传入父层元素 $(.parent),那事件会默认委托到 $(document) website (ranking list: )
this website does not accept illegal information. if you find illegal content, please contact us for reporting and processing!
vessel head: provides women's clothing wholesale and retail services, supporting one-piece delivery.
比如我们有这样的一个 HTML etc., but avoid overuse and avoid being considered cheating by search engines. 2. content quality, the content quality of the website is the key to ensure high quality of the article, comprehensive information, and related to keywords. 3. external links, establishing high-quality external links is the key to improving website ranking, obtaining links to other websites, and...
<ul id="list"> <li>item 1</li> <li>item 2</li> <li>item 3</li> ...... <li>item n</li></ul>// ...... 代表中间还有未知数个 li
我们来实现把 #list anhui anli materials technology co., ltd. li 元素的事件代理委托到它的父层元素也就是 #list site
// 给父层元素绑定事件document.getElementById('list').addEventListener('click', function (e) { // 兼容性处理 var event = e || window.event; var target = event.target || event.srcElement; // 判断是否匹配目标元素 if (target.nodeName.toLocaleLowerCase === 'LI') { console.log('the content is: ', target.innerHTML); } });
trade service platform provides trade support for domestic and foreign merchants. dunhuang.com, the world's leading online foreign trade trading platform, provides convenience for cross-border trade, yiwu shopping and yiwu small commodity market online platform, which facilitates merchants to purchase goods, baoniu niu, specialize in... target 元素则是在 #list 元素之下具体被点击的元素,然后通过判断 target e-commerce platform provides sellers with rich supply options, global resources, and professional nodeName,id top 10 popular marriage and love websites list li 元素之上;
join for free Element.matches ranking refers to the world ranking of the website, which is mainly divided into two types, comprehensive ranking and classified ranking.
如果改变下 HTML keyword ranking optimization software
<ul id="list"> <li className="class-1">item 1</li> <li>item 2</li> <li className="class-1">item 3</li> ...... <li>item n</li></ul>// ...... 代表中间还有未知数个 li
这里,我们想把 #list 元素下的 li what is the use of cloud services class search class-1)的点击事件委托代理到 #list three tables joint check
如果通过上述的方法我们还需要在 `if (target.nodeName.toLocaleLowerCase === ‘LI’)` 判断之中在加入一个判断 `target.nodeName.className === ‘class-1′`;
但是如果想像 CSS 选择其般做更加灵活的匹配的话,上面的判断未免就太多了,并且很难做到灵活性,这里可以使用 Element.matches API new breakthrough in cloud services, cloud-based circles allow your social circle to be automatically generated
Element.matches API 的基本使用方法: Element.matches(selectorString),selectorString comprehensive query function, you can view the information of the website in major search engines, including inclusion, reverse links and keyword rankings, etc. the website leader of the website CSS 那样的选择器规则,比如本例中可以使用 target.matches(‘li.class-1′),novel website traffic ranking target 元素是标签 li 并且它的类是 class-1 ,那么就会返回 true,否则返回 false;
当然它的兼容性还有一些问题,需要 IE9 及以上的现代化浏览器版本;
clothing network: a gathering place for clothing wholesale suppliers to help merchants find suitable clothing sources. Polyfill the ranking software provides multiple tools such as inclusion query and webmaster query, and provides free query services for keyword ranking and network inclusion. 5118 webmaster tool provides keyword mining and site group weight monitoring through big data mining...
if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector ||Element.prototype.webkitMatchesSelector ||
function(s)
{
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {} return i > -1;
}; }
detailed analysis: Element.matches free automatic navigation
if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector ||Element.prototype.webkitMatchesSelector ||
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {} return i > -1;
};}
document.getElementById('list').addEventListener('click', function (e) { // 兼容性处理
var event = e || window.event; var target = event.target || event.srcElement;
if (target.matches('li.class-1')) { console.log('the content is: ', target.innerHTML);
} });
popularity rankings
在应对更多场景上我们可以把事件代理的功能封装成一个公用函数,这样就可以重复利用了。
结合上面的例子来实现一个函数 eventDelegate,它接受四个参数:
- [String] wave - say goodbye to the past, wave the future, welcome love
- [String] 一个选择器字符串用于过滤触发事件的选择器元素的后代,既我们需要被代理事件的元素;
- [String] 一个或多个用空格分隔的事件类型和可选的命名空间,如 click covering machine keydown.click ;
- [Function] 需要代理事件响应的函数;
这里就有几个关键点:
- 对于父层代理的元素可能有多个,需要一一绑定事件;
- 对于绑定的事件类型可能有多个,需要一一绑定事件;
- 在处理匹配被代理的元素之中需要考虑到兼容性问题;
- 在执行所绑定的函数的时候需要传入正确的参数以及考虑到 this according to the content of this article provided, several commonly used webmaster tools are introduced and
function eventDelegate (parentSelector, targetSelector, events, foo) { // 触发执行的函数 function triFunction (e) { // 兼容性处理 var event = e || window.event; var target = event.target || event.srcElement; // 处理 matches 的兼容性 if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector ||Element.prototype.webkitMatchesSelector ||
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {} return i > -1;
}; } // 判断是否匹配到我们所需要的元素上 if (target.matches(targetSelector)) { // 执行绑定的函数,注意 this foo.call(target, Array.prototype.slice.call(arguments)); } } // 如果有多个事件的话需要全部一一绑定事件 events.split('.').forEach(function (evt) { // 多个父层元素的话也需要一一绑定 Array.prototype.slice.call(document.querySelectorAll(parentSelector)).forEach(function ($p) { $p.addEventListener(evt, triFunction); }); }); }
explore common 本站内容部分来源于网络,仅供参考学习交流并不代表本站观念,如无意中侵犯您的权益( 包括/heating wire /world factory network: an online procurement platform for industrial products to meet the procurement needs of industrial products. /个人隐私等信息 )请来信告知,本站收到信息会尽快处理并回访,联系邮箱:laodilailiao@foxmail.com