简单封装jq方法

简单封装jq方法

(function () {
    function _xQuery(select){
        this.nodes=document.querySelectorAll(select);
        if(this.nodes.length){
            for(var i=0;i<this.nodes.length;i++){
                this[i]=this.nodes[i]
            }
        }else{
            this[0]=this.nodes;
        }
    }
    //===================================
    function xQuery(select) {
        //设置样式
        function setStyle(dom,property,value) {
            for(var i=0;i<dom.length;i++){
                dom[i].style[property]=value
            }
        }
        // 获取样式
        function getStyle(dom,property) {
            return window.getComputedStyle(dom,null)[property];
        }
        _xQuery.prototype.css=function () {
            if(arguments.length==2){
                //$("p") .css("属性名","属性值")
                setStyle(this.nodes,arguments[0],arguments[1])
            }else if(arguments.length==1){
                if(typeof arguments[0]=="string"){
                    //$("p") .css("属性名")
                    return getStyle(this.nodes[0],arguments[0]);
                }else if(typeof arguments[0]=="object"){
                    //$("p") .css({"属性名":"属性值","属性名":"属性值"})
                    for(var key in arguments[0]){
                        setStyle(this.nodes,key,arguments[0][key])
                    }
                    
                }
            }

        };
        //html获取与设置节点内容
        _xQuery.prototype.html=function () {
            if(arguments.length==0){
                //    $("p").html() 获取节点的值
                // console.log("000")
                return this.nodes[0].innerHTML
            }else if(arguments.length>0){
                //    $("p").html("***") 设置节点的值
                // console.log("111")
                for(var i=0;i<this.nodes.length;i++){
                    this.nodes[i].innerHTML=arguments[0]
                }
            }
        };
        //text获取与设置节点内容
        _xQuery.prototype.text=function () {
            if(arguments.length==0){
                //    $("p").html() 获取节点的值
                // console.log("000")
                return this.nodes[0].innerText
            }else if(arguments.length>0){
                //    $("p").html("***") 设置节点的值
                // console.log("111")
                for(var i=0;i<this.nodes.length;i++){
                    this.nodes[i].innerText=arguments[0]
                }
            }
        };
        //val获取与设置节点内容
        _xQuery.prototype.val=function () {
            if(arguments.length==0){
                //    $("p").html() 获取节点的值
                // console.log("000")
                return this.nodes[0].value
            }else if(arguments.length>0){
                //    $("p").html("***") 设置节点的值
                // console.log("111")
                for(var i=0;i<this.nodes.length;i++){
                    this.nodes[i].value=arguments[0]
                }
            }
        };
        return new _xQuery(select);
    }
    xQuery.ajax=function () {

    };
    xQuery.post=function () {

    };
    //函数对象添加方法
    xQuery.extend=function (obj) {
        for(var key in obj){
            xQuery[key]=obj[key]
        }
    };
    //给实例对象添加方法
    xQuery.fn=_xQuery.prototype;
    xQuery.fn.extend=function (obj) {
        for(var key in obj){
            _xQuery.prototype[key]=obj[key]
        }
    };
    window.$ = window.xQuery = xQuery;
})();

这里只进行了简单的封装,有不足之处请多多指点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值