kingUtils.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * 实现jquery/zepto的方法。
  3. */
  4. ;(function(window,document , realJQuery){
  5. if(realJQuery){
  6. return ;
  7. }
  8. var Utils = function(selector , context ) {
  9. return new Utils.prototype.init(selector , context , rootjQuery);
  10. }
  11. var rootjQuery = Utils(document),
  12. rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
  13. Utils.fn = Utils.prototype = {
  14. init: function(selector, context, rootjQuery) {
  15. var match, elem, ret, doc;
  16. // Handle $(""), $(null), $(undefined), $(false)
  17. if ( !selector ) {
  18. return this;
  19. }
  20. // Handle $(DOMElement)
  21. if ( selector.nodeType ) {
  22. this.context = this[0] = selector;
  23. this.length = 1;
  24. return this;
  25. }
  26. // Handle HTML strings
  27. if ( typeof selector === "string" ) {
  28. if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
  29. // Assume that strings that start and end with <> are HTML and skip the regex check
  30. match = [ null, selector, null ];
  31. } else {
  32. match = rquickExpr.exec( selector );
  33. }
  34. // Match html or make sure no context is specified for #id
  35. // match[1]不为null,则为html字符串,match[2]不为null,则为元素id
  36. if ( match && (match[1] || !context) ) {
  37. // HANDLE: $(html) -> $(array)
  38. if ( match[1] ) {
  39. return jQuery.merge( this, selector );
  40. } else {
  41. elem = document.getElementById( match[2] );
  42. // Check parentNode to catch when Blackberry 4.6 returns
  43. // nodes that are no longer in the document #6963
  44. if ( elem && elem.parentNode ) {
  45. // Handle the case where IE and Opera return items
  46. // by name instead of ID
  47. // ie6,7和Opera存在此bug,当一个标签name和一个标签id值相等时,
  48. // document.getElementById(#id)函数将返回提前出现的标签元素
  49. if ( elem.id !== match[2] ) {
  50. // 如果存在以上Bug,则返回由find函数返回的document文档的后代元素集合
  51. return rootjQuery.find( selector );
  52. }
  53. // Otherwise, we inject the element directly into the jQuery object
  54. this.length = 1;
  55. this[0] = elem;
  56. }
  57. this.context = document;
  58. this.selector = selector;
  59. return this;
  60. }
  61. // HANDLE: $(expr, $(...))
  62. // context不存在或者context为jQuery对象
  63. } else if ( !context || context.jquery ) {
  64. return ( context || rootjQuery ).find( selector );
  65. // HANDLE: $(expr, context)
  66. // (which is just equivalent to: $(context).find(expr)
  67. // context为className或者dom节点元素
  68. } else {
  69. // 等同于jQuery(context).find(selector)
  70. return this.constructor( context ).find( selector );
  71. }
  72. // 处理$(fn)===$(document).ready(fn)
  73. } else if ( jQuery.isFunction( selector ) ) {
  74. return rootjQuery.ready( selector );
  75. }
  76. // 处理$(jQuery对象)
  77. if ( selector.selector !== undefined ) {
  78. this.selector = selector.selector;
  79. this.context = selector.context;
  80. }
  81. // 当第一个参数selector为jQuery对象时,将selector中的dom节点合并到this对象中,并返回this对象
  82. return jQuery.makeArray( selector, this );
  83. }
  84. }
  85. Utils.fn.init.prototype = Utils.fn;
  86. })(window , document , jQuery);