js的常用方法

var Jade = {};

Jade.util = {

    addEvent: function (target, eventType, eventHandle, param) {

        //deal param

        var regEventHandle = eventHandle

        if(param) {

            regEventHandle = function() {

                eventHandle.call(this, param);

            }

        }



        if (! +"\v1") {

            target['e' + eventType + regEventHandle] = regEventHandle;

            target.attachEvent('on' + eventType, function () {

                target['e' + eventType + regEventHandle]();

            });

        } else {

            target.addEventListener(eventType, regEventHandle, false);

        }

    }, // addEvent

    // detach event

    removeEvent: function (target, eventType, eventHandle) {

        if (target.removeEventListener) { //w3c

            target.removeEventListener(eventType, eventHandle, false);

        } else if (target.detachEvent) { // ie

            target.detachEvent("on" + eventType, eventHandle);

        } else {

            target["on" + eventType] = null;

        }

    }, // removeEvent

    addEvents: function (target, eventTypes, fn) {

        types = types.split(",");

        for (var i = 0; i < types.length; i++) {

            this.addEvent(target, types[i], fn);

        }

    }, // addEvents



    $$ : function(id) {

        return !id ? null : document.getElementById(id);

    },



    insertRange : function(el,content) {

        var range,curPosition;



        if(! +"\v1") {

            range = document.selection.createRange();

            range.moveStart("character", -el.value.length);

            curPosition = range.text.length;

        }else {

            curPosition = el.selectionStart;

        }



        var v = el.value;

        el.value = v.substr(0,curPosition) + content + v.substr(curPosition,v.length);

    },//textRange



    $C : function(classname,ele,tag) {

        var returns = [];

        ele = ele || document;

        tag = tag || '*';



        if(ele.getElementsByClassName) {

            var eles = ele.getElementsByClassName(classname);

            if(tag != '*') {

                for(var i=0, L=eles.length; i<L; i++) {

                    if(eles[i].tagName.toLowerCase() == tag.toLowerCase()) {

                        returns.push[eles[i]];

                    }

                }

            }else {

                returns = eles;

            }

        }else {

            eles = ele.getElementsByTagName(tag);

            var pattern = new RegExp("(^|\\s)"+classname+"(\\s|$)");

            for (i = 0, L = eles.length; i < L; i++) {

                    if (pattern.test(eles[i].className)) {

                            returns.push(eles[i]);

                    }

            }

        }

        

        return returns;

    },//getElementsByClassName



    

};



Jade.webhttp = {

   

}

 

你可能感兴趣的:(常用方法)