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.