JS模拟$
function $( v ){ if( typeof v === 'function' ){ window.onload = v; //$(function(){}) } else if ( typeof v === 'string' ) { return document.getElementById(v); //$("btn1") } else if ( typeof v === 'object' ) { return v; //this } }
封装后写法:
1、window.onload=function(){} 改写为 $( function(){})
2、document.getElementById( ) 改写为 $("div")
3、this 改写为 $( this )