首页 新闻 赞助 找找看

为什么我把jq的部分源码拿过来.却用不了呢?

0
悬赏园豆:5 [待解决问题]
 1 (function(window, undefined ) {
 2             
 3             var _$ = window.$,
 4 
 5             rootjQuery,
 6 
 7             // Support: IE<9
 8             // For `typeof node.method` instead of `node.method !== undefined`
 9             core_strundefined = typeof undefined,
10 
11             _jQuery = window.jQuery,
12 
13             core_version = "1.9.1",
14 
15             // A simple way to check for HTML strings
16             // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
17             // Strict HTML recognition (#11290: must start with <)
18             rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
19 
20             // Match a standalone tag
21             rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
22 
23             jQuery = function( selector, context ) {
24                 // The jQuery object is actually just the init constructor 'enhanced'
25                 return new jQuery.fn.init( selector, context, rootjQuery );
26             };
27 
28             jQuery.fn = jQuery.prototype = {
29                 // The current version of jQuery being used
30                 jquery: core_version,
31 
32                 constructor: jQuery,
33 
34                 init: function( selector, context, rootjQuery ) {
35                     var match, elem;
36 
37                     // HANDLE: $(""), $(null), $(undefined), $(false)
38                     if ( !selector ) {
39                         return this;
40                     }
41 
42                     // Handle HTML strings
43                     if ( typeof selector === "string" ) {
44                         if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
45                             // Assume that strings that start and end with <> are HTML and skip the regex check
46                             match = [ null, selector, null ];
47 
48                         } else {
49                             match = rquickExpr.exec( selector );
50                         }
51 
52                         // Match html or make sure no context is specified for #id
53                         if ( match && (match[1] || !context) ) {
54 
55                                 elem = document.getElementById( match[2] );
56 
57                                 // Check parentNode to catch when Blackberry 4.6 returns
58                                 // nodes that are no longer in the document #6963
59                                 if ( elem && elem.parentNode ) {
60                                     // Handle the case where IE and Opera return items
61                                     // by name instead of ID
62                                     if ( elem.id !== match[2] ) {
63                                         return rootjQuery.find( selector );
64                                     }
65 
66                                     // Otherwise, we inject the element directly into the jQuery object
67                                     this.length = 1;
68                                     this[0] = elem;
69                                 }
70 
71                                 this.context = document;
72                                 this.selector = selector;
73                                 return this;
74                             
75                         } 
76                 }
77             }
78         }
79 
80             // All jQuery objects should point back to these
81             rootjQuery = jQuery(document);
82 
83             // Limit scope pollution from any deprecated API
84             // (function() {
85 
86                         // })();
87             // Expose jQuery to the global object
88             window.jQuery = window.$ = jQuery;
89             return jQuery;
90             
91 })(window);

我是把jq的获取对象的入口代码拿下来...
<div class="" id="demo"></div>
console.log($("#demo"))  //这里根本获取不到jq对象

深蓝色梦想的主页 深蓝色梦想 | 初学一级 | 园豆:6
提问于:2013-07-04 18:48
< >
分享
所有回答(3)
0

报什么错?

二十三号同学 | 园豆:974 (小虾三级) | 2013-07-04 21:52
0

jquery的选择器是依赖sizzle引擎的,你没有sizzle引擎肯定是不行的。

如果从代码上说,那么你的代码抽离的不完全,比如像:

63                                         return rootjQuery.find( selector );

这行代码的find方法就没有定义(据我所知,find方法是依赖于sizzle的),因此不行。

如果仅仅是想做一个简易选择器,建议直接自己写一个,jquery的选择器还是挺复杂的,如果想抽离jquery的核心,建议看看github上jquery项目源码的core模块。

 

天涯无尘 | 园豆:196 (初学一级) | 2013-07-04 23:50

如果我用的是id選擇器,63行應該都沒執行到啊 .

支持(0) 反对(0) 深蓝色梦想 | 园豆:6 (初学一级) | 2013-07-05 09:55

@深蓝色梦想: 

18             rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
53                         if ( match && (match[1] || !context) ) {
55                                 elem = document.getElementById( match[2] );

好,单从代码分析,看这三行,当正则匹配到"#id"这种情况时,match[2]是空的,因为match[2]匹配的是"id"这种情况,如果你直接用"id"这种形式,则可以获取到elem的值~

支持(0) 反对(0) 天涯无尘 | 园豆:196 (初学一级) | 2013-07-05 10:54

@天涯无尘: 我就是用"id"这种形式,,就是获取不到elem的值

支持(0) 反对(0) 深蓝色梦想 | 园豆:6 (初学一级) | 2013-07-05 11:23

@深蓝色梦想: 

console.log($("#demo"))  //这里根本获取不到jq对象

你用的是"#id"这种吧。。。

支持(0) 反对(0) 天涯无尘 | 园豆:196 (初学一级) | 2013-07-05 17:44
0

<script src="jsScripts/jquery-1.9.1.js" type="text/javascript"></script>

<script src="js/jquery.query.js" type="text/javascript"></script>是不是这个jquery包没有引过来

茉莉花落 | 园豆:211 (菜鸟二级) | 2013-10-22 16:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册