Js learning Summary: How to Solve repeated problems in DOM2 compatibility, jsdom2

Source: Internet
Author: User

Js learning Summary: How to Solve repeated problems in DOM2 compatibility, jsdom2

DOM2 compatibility solves repeated problems as follows:

After solving this problem, you only need to make a judgment every time you add an event to the custom attribute and event pool. The specific code is as follows:

/* Bind: compatibility problem (binding method) for DOM2-level event binding @ parameter: curEle-> evenType, the element of the event to be bound-> event type to be bound ("click ", "mouseover") evenFn-> method to bind */function bind (curEle, evenType, evenFn) {if ('addeventlistener 'in document) {curEle. addEventListener (evenType, evenFn, false); return;} // apply the makeup to evenFn and paste the photo before the makeup into your brain var tempFn = function () {evenFn. call (curEle)} tempFn. photo = evenFn; // first determine whether a custom attribute exists before it exists. If it does not exist, create one. Because we want to store the results of multiple make-ups, let us set the value. Is an array if (! CurEle ["mybind" + evenType]) {// The array curEle ["mybind" + evenType] = [] according to different event types;} // solves the repeated problem: before you add a custom attribute to a container, check whether the container already exists. If yes, you do not need to add it again, similarly, you do not need to store var ary = curEle ["mybind" + evenType]; for (var I = 0; I <ary. length; I ++) {var cur = ary [I]; if (cur. photo === evenFn) {return ;}} ary. push (tempFn); curEle. attachEvent ("on" + evenType, tempFn); // The starting idea here is to change the point of this and not point this to window/* box. although attachEvent ("onclick", function () {fn1.call (box)}) solves this problem, it throws a new problem, I don't know how to delete it (we don't know who the anonymous function is) var tempFn = function () {fn1.call (box)} box. attachEvent ("onclick", tempFn); box. detachEvent ("onclick", tempFn); */} function unbind (curEle, evenType, evenFn) {if ('removeeventlistener 'in document) {curEle. removeEventListener (evenType, evenFn, false); return;} // retrieve the makeup result from evenFn to curEle ["myBind, find and then remove the makeup result from the event pool. Event pool var ary = curEle ['mybind' + evenType]; for (var I = 0; I <ary. length; I ++) {if (ary [I]. photo = evenFn) {ary. splice (I, 1) // find and remove the corresponding curEle from the container you stored. detachEvent ("on" + evenType, ary [I]); // remove the corresponding break from the event pool ;}}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.