js 模仿jquery 寫個簡單的小demo

<div id="div" style="background:red;width:100px;height:300px">
123123123
</div>

<script>
(function (window) {
    function Jquery(arguments) {
        this.obj = document.getElementById(arguments);
    }
    function $(arguments){
        return new Jquery(arguments);
    }
    Jquery.prototype.bgColor = function(arguments){
        this.obj.style.background=arguments;
    };
    Jquery.prototype.fontColor = function(arguments){
        this.obj.style.color=arguments;
    };
    window.Jquery=window.$=$;
})(window)

$('div').bgColor("red");
$('div').fontColor("white");

</script>

 寫的都是方法,心中有對象this

相關文章
相關標籤/搜索