this[0].nodeType !== 1 ? undefined :
//如果取的是input的value
(name == 'value' && this[0].nodeName == 'INPUT') ? this.val() :
//注意直接定义在node上的属性,在标准浏览器和ie9,10中用getAttribute取不到,得到的结果是null
//比如div.aa = 10,用div.getAttribute('aa')得到的是null,需要用div.aa或者div['aa']这样来取
(!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result) :
this.each(function(idx) {
if (this.nodeType !== 1) return
//如果name是一个对象,如{'id':'test','value':11},则给数据设置属性
if (isObject(name)) for (key in name) setAttribute(this, key, name[key])
//如果name只是一个普通的属性字符串,用funcArg来处理value是值或者function的情况最终返回一个属性值
//如果funcArg函数返回的是undefined或者null,则相当于删除元素的属性
else setAttribute(this, name, funcArg(this, value, idx, this.getAttribute(name)))
})
},
removeAttr: function(name) {
return this.each(function() {
this.nodeType === 1 && setAttribute(this, name)//setAttribute的第三个参数为null时,效果是删除name属性
})
},
//获取第一条数据的指定的name属性或者给每条数据添加自定义属性,注意和setAttribute的区别
prop: function(name, value) {
//没有给定value时,为获取,给定value则给每一条数据添加,value可以为值也可以是一个返回值的函数
return (value === undefined) ? (this[0] && this[0][name]) : this.each(function(idx) {
this[name] = funcArg(this, value, idx, this[name])
})
},
data: function(name, value) {
//通过调用attr方法来实现获取与设置的效果,注意attr方法里,当value存在的时候,返回的是集合本身,如果不存在,则是返回获取的值
var data = this.attr('data-' + dasherize(name), value)
return data !== null ? deserializeValue(data) : undefined
},
val: function(value) {
return (value === undefined) ?
//如果是多选的select,则返回一个包含被选中的option的值的数组
(this[0] && (this[0].multiple ? $(this[0]).find('option').filter(function(o) {
return this.selected
}).pluck('value') : this[0].value)) : this.each(function(idx) {
this.value = funcArg(this, value, idx, this.value)
})
},
offset: function(coordinates) {
if (coordinates) return this.each(function(index) {
var $this = $(this),
//coordinates为{}时直接返回,为函数时返回处理结果给coords
coords = funcArg(this, coordinates, index, $this.offset()),
//取父级的offset
parentOffset = $this.offsetParent().offset(),
//计算出它们之间的差,得出其偏移量
props = {
top: coords.top - parentOffset.top,
left: coords.left - parentOffset.left
}
//注意元素的position为static时,设置top,left是无效的
if ($this.css('position') == 'static') props['position'] = 'relative'
$this.css(props)
})
//取第一条记录的offset,包括offsetTop,offsetLeft,offsetWidth,offsetHeight
if (this.length == 0) return null
var obj = this[0].getBoundingClientRect()
//window.pageYOffset就是类似Math.max(document.documentElement.scrollTop
关键词:zepto是啥