共计 2890 个字符,预计需要花费 8 分钟才能阅读完成。
最近开始学习 Zepto 源代码,分享出来,共同学习。前端新人,技术欠佳,多多见谅
参考的 gitbook 地址
感谢作者奉献
zepto 的 css 选择器zepto.qsa
我们都知道,很多时候,我们都用 $
来获取 DOM 对象,这跟 zepto.qsa
有很大的关系。
获取 selector[0]
如果为 ID getElementById 处理
如果为 calss getElementsByClassName 处理
如果为 tagName getElementsByTagName 处理
非但选择器 querySelectorAll 处理
最后选择才调用 querySelectorAll 是因为 querySelectorAll 耗费性能太高
参考静态的 nodelist 对比动态的 nodelist
zepto.qsa = function (element, selector) {
var found,
maybeID = selector[0] == '#',
maybeClass = !maybeID && selector[0] == '.',
nameOnly = maybeID || maybeClass ? selector.slice(1) : selector, // Ensure that a 1 char tag name still gets checked
isSimple = simpleSelectorRE.test(nameOnly)
return (element.getElementById && isSimple && maybeID) ? // Safari DocumentFragment doesn't have getElementById
((found = element.getElementById(nameOnly)) ? [found] : []) :
(element.nodeType !== 1 && element.nodeType !== 9 && element.nodeType !== 11) ? [] :
slice.call(
isSimple && !maybeID && element.getElementsByClassName ? // DocumentFragment doesn't have getElementsByClassName/TagName
maybeClass ? element.getElementsByClassName(nameOnly) : // If it's simple, it could be a class
element.getElementsByTagName(selector) : // Or a tag
element.querySelectorAll(selector) // Or it's not simple, and we need to query all
)
}
zepto.Z 函数
返回 new Z 函数
zepto.Z = function (dom, selector) {return new Z(dom, selector)
}
Z 函数 返回数组
function Z(dom, selector) {
var i, len = dom ? dom.length : 0
for (i = 0; i < len; i++) this[i] = dom[i]
this.length = len
this.selector = selector || ''
}
isZ 判断是否是 Z 函数
zepto.isZ = function (object) {return object instanceof zepto.Z}
zepto.init 初始化函数
zepto.init = function (selector, context) {
var dom
// If nothing given, return an empty Zepto collection
if (!selector) return zepto.Z()
// Optimize for string selectors
else if (typeof selector == 'string') {selector = selector.trim()
// If it's a html fragment, create nodes from it
// Note: In both Chrome 21 and Firefox 15, DOM error 12
// is thrown if the fragment doesn't begin with <
if (selector[0] == '<' && fragmentRE.test(selector))
dom = zepto.fragment(selector, RegExp.$1, context), selector = null, console.log(dom)
// If there's a context, create a collection on that context first, and select
// nodes from there
else if (context !== undefined) return $(context).find(selector)
// If it's a CSS selector, use it to select nodes.
else dom = zepto.qsa(document, selector), console.log(dom, 123123);
}
// If a function is given, call it when the DOM is ready
else if (isFunction(selector)) return $(document).ready(selector)
// If a Zepto collection is given, just return it
else if (zepto.isZ(selector)) return selector
else {
// normalize array if an array of nodes is given
if (isArray(selector)) {dom = compact(selector)
console.log(dom);
}
// Wrap DOM nodes.
else if (isObject(selector))
dom = [selector], selector = null
// If it's a html fragment, create nodes from it
else if (fragmentRE.test(selector))
dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null
// If there's a context, create a collection on that context first, and select
// nodes from there
else if (context !== undefined) return $(context).find(selector)
// And last but no least, if it's a CSS selector, use it to select nodes.
else dom = zepto.qsa(document, selector)
}
// create a new Zepto collection from the nodes found
return zepto.Z(dom, selector)
}
正文完
发表至: javascript
2019-11-08