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 )

posted @ 2017-04-18 11:27  念念念不忘  阅读(74)  评论(0)    收藏  举报