+ -
当前位置:首页 → 问答吧 → 模拟jQuery 框架的问题,求高手帮忙。

模拟jQuery 框架的问题,求高手帮忙。

时间:2011-10-22

来源:互联网

HTML code

<!doctype html>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div id="a">a</div>
        <div id="b">b</div>
        <script>
            (function(){
                var j = function(o){
                    return j.fn.init(o);
                };
                j.fn = j.prototype = {
                    init: function(o){
                        this[0] = document.getElementById(o);
                        this.length = 0;
                        return this
                    },
                    html: function(str){
                        if(str!=null){
                            this[0].innerHTML = str;
                            return this
                        }
                        return this[0].innerHTML;
                    },
                    size:function(){
                        return this.length;
                    }
                }
                window.$ = j;
            })()
            
            //alert( $('a').html() ) //a 框架成功    
            
            var b = $('b').html()
            //$('a').html( b )  // 这样能够赋值成功
            $('a').html( $('b').html() )  //但是 我不想用变量过渡,怎么用这样方式。
            //我看了一下 jquery中 用isFunction来判断传来的变量,具体怎么实现的,不懂。
            // 也就是说 怎么判断 html( str ) 中 str是否为函数,如果是函数,先运行它,再给innerHTML 赋值
        </script>
    </body>
</html>




上述问题,求高手帮忙。

作者: zgzglike   发布时间: 2011-10-22

人工置顶。

作者: zgzglike   发布时间: 2011-10-22

思路很正确,单isFunction 不就是运用 toString.call(obj) === "[object Function]" 来判断的?

作者: xiaojing7   发布时间: 2011-10-22