Micro class library g

Source: Internet
Author: User

I haven't updated it for a long time. Recently, my work has changed frequently and is still impetuous! It is not easy to engage in technology with peace of mind these years.

Now the company is relatively idle to sort out the previous Code public repository and pop-up layer.

Put the sorted public repository first (for memo). The structure is very simple or even simple, and the function has almost no changes, mainly because the usage is simpler than before, similar to jquery APIs, it supports Simple chained operations. All methods and attributes are attached to the g object. For the reason why the name g is used, the first character of the original getEl method is used because the name is short and $ is not used.

Ps: I found that Kingdee's public database also uses getEl as the selector function name (ah, it is a bit sad to Miss Kingdee's interview ).

There is so much nonsense. The following is the code. I believe that this easy-to-understand function library does not require the complexity of api documentation! In addition, there are some brief notes. If you do not understand it, follow the post to ask a question.

Usage:

G ('# test'). children (). each (function (){
Alert (this. nodeName );
});
G.xhr('loadurl', 'abc.txt ', function (data ){
Var json = eval ('+ data +'), str = '', nl;
Nl = json. nodeList;
For (var I = 0, len = nl. length; I <len; I ++ ){
Str + = '<p> <strong>' + I + '.' + nl [I]. p + '</strong> </p> ';
}
G('{result'{.html (str );
});
The // g () selector returns the encapsulated object. Currently, only the id/css/tagName and children () parameters are not supported, and change the dom element of the current object,
// The each function can be used to cyclically output the selected dom element list or array object.
// Xhr creates an ajax object. The first parameter can be the loadUrl method or the postData method, the second parameter is the url, and the third parameter is the callback method, the fourth parameter is post data.

G. js original code:

/* Author: hotcreateDate: 2010-10-11update: 2011-8-30JavaScript simple library */(function (window, undefined) {var g = function (selecter) {// simple class library if (! (This instanceof g) return new g (selecter); if (selecter instanceof g) return selecter; this. dom = selecter? This. init (selecter): []; // native dom element this. length = this. dom. length; return this ;}; g.info = {create: "2010-10-11", modify: "2011-08-30", version: "1.0.1"}; g. prototype = {init: function (selecter) {// simple selector. You can select id, css, and tag switch (typeof selecter) {case 'string ': if (/^ # ([\ w-] +) $ /. test (selecter) // match the id selector return [document. getElementById (selecter. replace ('#', '')]; else if (/^ \. ([\ w-] +) $ /. test (selecter) {// matches the css selector and returns the array set if (docu Ment. getElementsByClassName) return document. getElementsByClassName (selecter. replace ('.', ''); else {// The following is compatible with ie5.5var allEl = document. all? Document. all: document.doc umentElement. getElementsByTagName ("*"), result = []; for (var I = 0, len = allEl. length; I
 
  
-1) result. push (allEl [I]);} return result ;}} else if (/^ ([\ w-] +) $ /. test (selecter) {// match the tag selector var els = document. getElementsByTagName (selecter ). length = 0? []: Document. getElementsByTagName (selecter); return els;} elsereturn []; case 'object': // return selecter. length directly? Selecter: [selecter]; default: return [] ;}}, each: function (fun) {// loop if (! G. isFunction (fun) return; if (this. dom. length) {for (var I = 0, len = this. dom. length; I
  
   
0;}, addEvent: function (type, fn) {// bind event this. each (function () {g. event (). add (this, type, fn) ;}, removeEvent: function (type, fn) {// remove binding this. each (function () {g. event (). remove (this, type, fn) ;}, hover: function (fnOver, fnOut) {// move the mouse over the function if (! G. isFunction (fnOver) |! G. isFunction (fnOut) return; this. addEvent ('mouseover', fnOver); this. addEvent ('mouseout', fnOut);}, position: function () {// absolute position of the dom node if (this. dom [0] & this. dom [0]. nodeType = 1) {var left = 0, top = 0, right = 0, bottom = 0; // getBoundingClientRect of ie8 gets inaccurate if (! This. dom [0]. getBoundingClientRect | g. browser. ie8) {var n = this. dom [0]; while (n) {left + = n. offsetLeft, top + = n. offsetTop; n = n. offsetParent;}; right = left + n. offsetWidth; bottom = top + n. offsetHeight;} else {var rect = this. dom [0]. getBoundingClientRect (); left = right = g. getScrollLeft (this. dom [0]); top = bottom = g. getScrollTop (this. dom [0]); left + = rect. left; right + = rect. right; top + = re Ct. top; bottom + = rect. bottom ;}; return {"left": left, "top": top, "right": right, "bottom": bottom };}, css: function () {// set the current style attribute var str0 = arguments [0], str1 = arguments [1]; if (arguments. length = 1 & g. isString (str0) {return g. style (this. dom [0], str0);} else if (arguments. length = 1 & typeof (str0) = 'object') {for (var name in str0) {g. style (this. dom [0], name, str0 [name]) ;}} else if (arguments. length = 2 & g. isString (Str0) & g. isString (str1) {g. style (this. dom [0], str0, str1);} return this ;}, size: function () {// obtain the width and height of an element, including var dom = this of the hidden element. dom [0]; var width = dom. offsetWidth, height = dom. offsetHeight; if (! Width &&! Height) {var clone = dom. cloneNode (false); dom. parentNode. appendChild (clone); var style = clone. style; var cssShow = "position: absolute; visibility: hidden; display: block; left:-9999px; top:-9999px;"; // var cssBack = "position: "+ style. position + "; visibility:" + style. visibility + "; display:" + style. display + "; left:" + style. left + "; top:" +style.top;clone.style.css Text = cssShow; width = clone. offsetWidth; height = clone. off SetHeight; dom. parentNode. removeChild (clone);} return {"width": width, "height": height };}, remove: function (elem) {// remove an internal element, it can also be itself var self = this; if (! Elem) {self. each (function () {this. parentNode. removeChild (this) ;}); this. dom = []; return this;} if (g (elem ). dom. length) {self. each (function () {var me = this; g (elem ). each (function () {if (g (me ). has (this) this. parentNode. removeChild (this) ;}) ;}return this ;}}; g. prototype. extend = g. extend = function () {var target = arguments [0] | |{}, I = 1, length = arguments. length, deep = false, options, name, src, copy; if (Typeof target = "boolean") {deep = target; target = arguments [1] |{}; I = 2 ;}if (typeof target! = "Object "&&! G. isFunction (target) {target = {};} if (length = I) {target = this; -- I ;}for (; I = 0? (ParseFloat (/opacity = ([^)] *)/. exec (el. style. filter) [1])/100) + ":" ";} if (set) {el. style [name] = value;} else {return getStyle (el, name);} function getStyle (elem, styleName) {// obtain the current style attribute if (elem. currentStyle) // iereturn styleName? Elem. currentStyle [styleName]: elem. currentStyle; else {// webkitvar arr = elem. ownerDocument. defaultView. getComputedStyle (elem, null); return styleName? Arr [styleName]: arr ;}}, stopEvent: function (e) {// block triggering default event e = e | window. event; if (e. preventDefault) {e. preventDefault (); e. stopPropagation ();} else {e. returnValue = false; e. cancelBubble = true ;}}, getScrollTop: function (node) {// get the actual location of the node on the page topvar doc = node? Node. ownerDocument: document; return doc.doc umentElement. scrollTop | doc. body. scrollTop ;}, getScrollLeft: function (node) {// obtain the actual location of the node on the page. leftvar doc = node? Node. ownerDocument: document; return doc.doc umentElement. scrollLeft | doc. body. scrollLeft;}, cancelBubble: function (e) {// block event bubbling e = e | window. event; e. stopPropagation? E. stopPropagation (): e. cancelBubble = true;}, stopSelect: function () {// prevent the browser from selecting window. getSelection by default? Window. getSelection (). removeAllRanges (): document. selection. empty () ;}, event: function () {// native event compatibility handle var add, remove, guid = 1, base ={}; add = function (element, type, handler) {if (type = 'domainloaded' | type = 'ready') {g. ready (handler); return;} if (element. addEventListener) {element. addEventListener (type, handler, false);} else {if (! Handler. $ guid) {handler. $ guid = guid ++;} if (! Element. events) {element. events ={};} var handlers = element. events [type]; if (! Handlers) {handlers = element. events [type] ={}; if (element ['on' + type]) {handlers [0] = element ['on' + type] ;}} handlers [handler. $ guid] = handler; element ['on' + type] = handleEvent ;}}; remove = function (element, type, handler) {if (element. removeEventListener) {element. removeEventListener (type, handler, false);} else {if (element. events & element. events [type]) {delete element. Events [type] [handler. $ guid] ;}}; function handleEvent () {var returnValue = true, event = fixEvent (); var handlers = this. events [event. type]; for (var I in handlers) {this. $ handleEvent = handlers [I]; if (this. $ handleEvent (event) === false) {returnValue = false ;}} return returnValue;} function fixEvent (event) {if (event) return event; event = (this. ownerDocument | this.doc ument | this ). par EntWindow | window ). event; (function (calc) {event. pageX = event. clientX + calc ('left'); event. pageY = event. clientY + calc ('top');}) (function (side) {return (base ['scroll '+ side] | 0) -(base ['client' + side] | 0 has been used before using event.tar get = event. srcElement; switch (event. type) {case 'mouseout': event. relatedTarget = event. toElement; case 'mouseover': event. relatedTarget = event. fromElement;}; return e Vent ;}; return {'add': add, 'delete': remove, 'fixevent': fixEvent };}, forEach: function (arr, fun) {if (! G. isFunction (fun) return; if (g. isArray (arr) {for (var I = 0, len = arr. length; I
   
    
) [^ \ T] *) '/g, "$1 \ r"). replace (/\ t = (.*?) %>/G, "', $1 ,'"). split ("\ t "). join ("');"). split ("%> "). join ("p. push ('"). split ("\ r "). join ("\ '") + "');} return p. join ('');"); return data? Fn (data): fn1_1_1_1_1_1_1_1_try1_document.exe cCommand ("BackgroundImageCache", false, true);} catch (e) {}window. getEl = window. g = g;}) (window );
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.