1. Let’s talk about custom events today Everyone knows about events, but there are implementations of custom events in many frameworks. I wrote a simple one to share with you,
<script> <br>var cusEvent = function(){ <br>var cache = {}; <br>return { <br>addEvent:function(type,fn){ <br>cache[type]?cache[type].push(fn):(cache[type]= [fn]); <br>}, <br>removeEvent:function(type,fn){ <br>if(cache[type]){ <br>if(fn){ <br>for(var i=0 ,ci;ci=cache[type][i];i ){ <br>ci===fn&&cache[type].splice(i,1); <br>} <br>}else{ <br>delete cache [type]; <br>} <br>} <br>}, <br>//e can be a custom object or a string <br>fire:function(e){ <br>if( typeof e =='string'){ <br>e = {type:e} <br>}; <br>var t = cache[e.type]; <br>if(t){ <br>for( var i=0,ci;ci=t[i];i ){ <br>//e can have its own target, if not, use this instead <br>ci.call(e.target||this,e) <br>} <br>} <br>} <br>} <br>}() <br>//Use <br>cusEvent.addEvent('start',function(e){alert(e.type) }) <br>cusEvent.addEvent('start',function(e){alert(e.type "1")}) <br>cusEvent.fire('start') <br>cusEvent.removeEvent('start' ) <br></script>
2. Everyone has used innerHTML. It works well, but sometimes it doesn’t work under IE. For example, if you want to use option on select, just No, because the innerHTML of select is read-only. Of course, in addition to this, there are also elements such as tr, table, etc. I wrote a small method to be compatible with the use of these elements by innerHTML under IE. I hope it can give you some inspiration
The principle is that in IE, I use a temporary element div to skip innerHTML and it cannot For the problem of usage, you can write some more load points, that is, if it is judged that the passed in is tr or table, then apply it with the corresponding elements. This method can also solve the problem that it is difficult to add and modify the options in the select 3. The global g-add variable is a devil in js, and it is absolutely recommended not to use it. However, sometimes, you may have to write a static variable, which accumulates as the function executes, such as
4. The traditional way is to traverse through el.offsetParent and el.offsetLeft to obtain , but in fact the best and easier way is getBoundingClientRect The code is as follows
When you click on the gray div at the bottom, the red one above will completely overlap with the gray one 5. OuterHTML under ie Have you ever used it? Fat Sauce is easy to use. You not only want to return the html under a certain element, but also want to return the html of this element However, this attribute can only be used under IE. Other browsers do not have this attribute. , what to do, js tips help you solve this problem
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn