11/**
2- * @author : zyh
3- * @see : <a href="mailto:[email protected] ">zyh </a> 2+ * @author : 豪情
3+ * @see : <a href="mailto:[email protected] ">豪情 </a> 44 * @time : 2014-4-19 下午5:45
55 * @info :
66 */
77; ( function ( win , undefined ) {
8- var doc = win . document ,
8+ var document = win . document ,
99 loc = win . location ,
10- docEle = doc . documentElement ,
10+ docEle = document . documentElement ,
1111 arr = [ ] ,
1212 slice = arr . slice ,
1313 concat = arr . concat ,
1414 push = arr . push ,
15+ indexOf = arr . indexOf , // js 1.6新增加的方法
1516 class2type = { } ,
1617 toString = class2type . toString ,
1718 hasOwn = class2type . hasOwnProperty ,
3839 }
3940
4041 $ . fn = $ . prototype = {
41- jing : 1 ,
42+ jing : '1.0' ,
4243 constructor : $ ,
4344 sort : arr . sort ,
4445 push : push ,
4546 splice : arr . splice ,
4647 context : null ,
4748 init : function ( selector , context ) {
4849 var obj = null ,
49- context = context || doc ,
50+ context = context || document ,
5051 select = '' ;
5152
5253 // $(''), $(null), $(undefined), $(false)
7980 } else { // $("tagName")
8081 obj = context . getElementsByTagName ( selector ) ;
8182 }
82- this . context = doc ;
83+ this . context = document ;
8384
8485 return this . setArray ( this . makeArray ( obj ) ) ;
8586 } ,
8687 toArray : function ( ) {
8788 return slice . call ( this ) ;
8889 } ,
90+ /**
91+ * 如果num为负则返回this.length+num值;如果num为空,直接返回一个数组元素
92+ * @param num
93+ */
94+ get : function ( num ) {
95+ return num != null ? ( num < 0 ? this [ num + this . length ] : this [ num ] ) : slice . call ( this ) ;
96+ } ,
8997 setArray : function ( obj ) {
9098 this . length = 0 ;
9199 Array . prototype . push . apply ( this , obj ) ;
110118 return ret ;
111119 } ,
112120 getElementsByClassName : function ( context , name ) {
113- if ( ! doc . getElementsByClassName ) {
114- return doc . getElementsByClassName ( name ) ;
121+ if ( document . getElementsByClassName ) {
122+ return document . getElementsByClassName ( name ) ;
115123 } else {
116124 var regex = new RegExp ( '(^|\\s)' + name + '(\\s|$)' ) ,
117125 len = context . all . length ,
131139
132140 each : function ( callback ) {
133141 return $ . each ( this , callback ) ;
142+ } ,
143+ /**
144+ * 确定elem在数组中的位置,从0开始计数(如果没有找到则返回 -1 )。
145+ * @param elem
146+ * @param arr
147+ * @param i 开始位置
148+ */
149+ inArray : function ( elem , arr , i ) {
150+ var len ;
151+
152+ if ( arr ) {
153+ if ( indexOf ) {
154+ return indexOf . call ( elem , arr , i ) ;
155+ }
156+
157+ len = arr . length ;
158+ i = i ? i < 0 ? Math . max ( 0 , len + i ) : i : 0 ;
159+ for ( ; i < len ; i ++ ) {
160+ if ( i in arr && arr [ i ] == elem ) {
161+ return i ;
162+ }
163+ }
164+ }
165+
166+ return - 1 ;
134167 }
135168 }
136169
150183 for ( ; i < length ; i ++ ) {
151184 if ( ( options = arguments [ i ] ) != null ) {
152185 for ( name in options ) {
153- target [ name ] = options [ name ] ;
186+ if ( options [ name ] != undefined ) {
187+ target [ name ] = options [ name ] ;
188+ }
154189 }
155190 }
156191 }
246281 } ) ;
247282
248283 // DOM常规操作
249- $ . extend ( {
284+ $ . fn . extend ( {
250285 append : function ( ) {
251- return true ;
286+ return this . domMainp ( arguments , function ( elem ) {
287+ if ( this . nodeType == 1 || this . nodeType == 11 || this . nodeType == 9 ) {
288+ this . appendChild ( elem ) ;
289+ }
290+ } ) ;
291+ } ,
292+ prepend : function ( ) {
293+ return this . domMainp ( arguments , function ( elem ) {
294+ if ( this . nodeType == 1 || this . nodeType == 11 || this . nodeType == 9 ) {
295+ this . insertBefore ( elem , this . firstChild ) ;
296+ }
297+ } ) ;
298+ } ,
299+ before : function ( ) {
300+ return this . domMainp ( arguments , function ( elem ) {
301+ if ( this . parentNode ) {
302+ this . parentNode . insertBefore ( elem , this ) ;
303+ }
304+ } ) ;
305+ } ,
306+ after : function ( ) {
307+ return this . domMainp ( arguments , function ( elem ) {
308+ if ( this . parentNode ) {
309+ this . parentNode . insertBefore ( elem , this . nextSibling ) ;
310+ }
311+ } ) ;
252312 } ,
253313 /**
254- * 将args转换为dom元素,并放在一个文档碎片中,
314+ * dom处理, 将args转换为dom元素,并放在一个文档碎片中,
255315 * 执行callback,实现真正的回调插入操作
256316 * @param args
257- * @param table
258317 * @param callback
259318 */
260- domMainp : function ( args , table , callback ) {
261-
319+ domMainp : function ( args , callback ) {
320+ args = concat . apply ( [ ] , args ) ;
321+
322+ var i = 0 ,
323+ l = this . length ,
324+ fragment ,
325+ node ;
326+
327+ if ( l ) {
328+ // fragment = this.buildFragment(args, this[0].ownerDocument, false, this);
329+ var div = document . createElement ( 'div' ) ;
330+ div . innerHTML = args ;
331+ node = div . childNodes [ 0 ] ;
332+ div = null ;
333+
334+ for ( ; i < l ; i ++ ) {
335+ callback . call ( this [ i ] , node , i ) ;
336+ }
337+ }
338+
339+ return this ;
262340 } ,
263341 buildFragment : function ( elems , context , scripts , selection ) {
264342
273351 class2type [ '[object ' + name + ']' ] = name . toLowerCase ( ) ;
274352 } ) ;
275353
354+
355+ // className 相关操作
356+ var rnotwhite = ( / \S + / g) ;
357+ $ . fn . extend ( {
358+ addClass : function ( value ) {
359+ var i = 0 ,
360+ j = 0 ,
361+ cur = '' ,
362+ elem = null ,
363+ classes = [ ] ,
364+ clazz = '' ,
365+ len = this . length ,
366+ proceed = typeof value === 'string' && value ;
367+
368+ if ( proceed ) {
369+ classes = value . match ( rnotwhite ) ;
370+ for ( ; i < len ; i ++ ) {
371+ elem = this [ i ] ;
372+
373+ cur = elem . nodeType === 1 && elem . className ? ' ' + elem . className + ' ' : '' ;
374+ if ( cur ) {
375+
376+ }
377+ }
378+ }
379+ }
380+ } ) ;
381+
276382} ( window ) ) ;
277383
278384// 2014-04-21 : 准备开发第一版
279385// 2014-04-30 : 完成$.type, $.each等方法
386+ // 2014-05-04 : 增加$.get方法
387+ // 2014-05-05 : 以精减的方式添加:append,prepend,before,after方法,但存在tbody问题未处理;添加addClass
0 commit comments