Core methods
Z()
Z(selector, [context]) ⇒ collection
Z(<Z collection>) ⇒ same collection
Z(<DOM nodes>) ⇒ collection
Z(function(Z){ ... }) ⇒ DOM Ready
通过CSS选择器创建一个 Z
集合对象, 会将DOM元素包装起来.
一个 Z
对象是一个类数组(array-like)对象,它包含了一些可链式(chainable methods)操作DOM元素的方法。
如果指定了 context (CSS selector, DOM node), 那么将在 context 内部进行查找.
这等价于Z(context).find(selector)
.
如果 selector 是函数类型,则相当于 DOMContentLoaded
事件.
Z('#foo') //=> 获取id为foo的元素
Z('.bar') //=> 获取className为bar的元素集合
Z('div') //=> 获取页面里所有的DIV元素
Z('[name]') //=> 获取页面里具有name属性的元素集合
Z('[data-tag=nav]') //=> 获取页面里data-tag属性值为nav的元素集合
Z('.bar', '#foo') //=> 获取id为foo的元素里所有className为bar的元素集合
// execute callback when the page is ready:
Z.js(function(Z){
alert('Ready to Z.js!')
})
依据2/8原则,也为了使 z.js
足够轻量,IE8以下仅实现了最常用的id, className, tagName, attribute选择器。
find
find(selector) ⇒ collection
在指定元素内部查找匹配的css selector元素。
var form = Z('#myform')
form.find('input')
eq
eq(index) ⇒ collection
获取集合中指定位置的元素,返回的为一个Z
对象。
Z('li').eq(0) //=> 返回集合里第一个元素
Z('li').eq(-1) //=> 返回集合里最后一个元素
first
first() ⇒ self
获取集合里的第一个元素.
last
last() ⇒ collection
Get the last element of the current collection.
prev
prev() ⇒ self
获取当前元素的前一个兄弟元素。
next
next() ⇒ self
获取当前元素的后一个兄弟元素。
parent
parent() ⇒ self
获取当前元素的后父元素。
closest
closest(selector, [context]) ⇒ collection
向上查找符合选择器的元素,context指定查找的范围。注意与
parent()
的区别。
var input = Z('input[type=text]')
input.closest('form')
push
push(element, [element2, ...]) ⇒ self
从集合尾部添加元素。
sort
sort(function() { ... }) ⇒ self
排序元素。
slice
slice() ⇒ self
截取集合里的元素,同数组的slice。
splice
splice() ⇒ self
截取集合里的元素,同数组的splice。
get
get() ⇒ array
get(index) ⇒ DOM node
将集合元素对象转成DOM元素数组返回,如果传了index则只返回一个元素。
var elements = Z('h2')
elements.get() //=> get all headings as an array
elements.get(0) //=> get first heading node
toArray
toArray() ⇒ self
将Z
集合转成对应DOM元素的数组,即去掉wrapper。
each
each(function(item, index){ ... }) ⇒ self
迭代集合里的每一个element,this
指代当前的element,如果迭代函数内部返回true则立即停止迭代。
Z('form input').each(function(index){
console.log('input %d is: %o', index, this)
})
map
map(function(index, item){ ... }) ⇒ collection
Iterate through all elements and collect the return values of the iterator
function. Inside the iterator function,
this
keyword refers to the current
item (also passed as the second argument to the function).
Returns a collection of results of iterator function, with
null
and
undefined
values filtered out.
// get text contents of all elements in collection
elements.map(function(){ return Z(this).text() }).get().join(', ')
data
data(name) ⇒ value
data(name, value) ⇒ self
获取第一个元素的扩展属性的值,或者设置所有元素扩展属性。
var input = Z('input[type=text]')
input.data('mydata', 10)
input.data('mydata') //=> 10
removeData
removeData(name) ⇒ value
删除所有元素扩展属性。
var input = Z('input[type=text]')
input.removeData('mydata')
Z.ready
Z.ready(function(Z){ ... }) ⇒ self
DOM Ready (“DOMContentLoaded” )或页面load后执行,推荐使用Z() 替代该方法。
Z.extend
Z.extend(target, [source, [source2, ...]]) ⇒ target
Z.extend(true, target, [source, ...]) ⇒ target [v1.0]
扩展一个对象,会覆盖目标对象的已有属性值。默认是浅复制,第一个参数为 true
时为深复制。
var target = { one: 'patridge' },
source = { two: 'turtle doves' }
Z.extend(target, source)
//=> { one: 'patridge',
// two: 'turtle doves' }
Z.each
Z.each(collection, function(index, item){ ... }) ⇒ collection
遍历一个数组或JS对象,迭代函数内返回false
将停止迭代。
Z.each(['a', 'b', 'c'], function(item, index){
console.log('item %d is: %s', index, item)
})
var hash = { name: 'Z.js', size: 'micro' }
Z.each(hash, function(value, key){
console.log('%s: %s', key, value)
})
Z.map
Z.map(collection, function(item, index){ ... }) ⇒ collection
迭代一个集合,过滤null
和 undefined
后返回一个新集合。
Z.contains
Z.contains(parent, node) ⇒ boolean
判断元素parent里是否包含给定的元素node,包含返回true,否则返回false
Z.parseJSON
Z.parseJSON(string) ⇒ object
将字符串解析成JSON格式。
DOM操作
html
html() ⇒ string
html(content) ⇒ self
html(function(){ ... }) ⇒ self
获取第一个元素的innerHTML,或设置所有元素的innerHTML.
// autolink everything that looks like a Twitter username
Z('.comment').html('some html')
text
text() ⇒ string
text(content) ⇒ self
获取第一个元素所包含的文本内容,或设置所有元素的文本内容.
attr
attr(name) ⇒ string
attr(name, value) ⇒ self
attr({ name: value, name2: value2, ... }) ⇒ self
读取或设置元素的属性(attributes),多个属性可以通过对象方式设置。
获取 DOM properties 如 checked
或 selected
使用 prop。
var form = Z('form')
form.attr('action') //=> read value
form.attr('action', '/create') //=> set value
form.attr('action', null) //=> remove attribute
// multiple attributes:
form.attr({
action: '/create',
method: 'post'
})
removeAttr
removeAttr(name) ⇒ self
删除集合中元素指定的属性。
Z('.tab').removeAttr('data-tab')
hasAttr
hasAttr(name) ⇒ boolean
判断元素是否有指定name的属性.
prop
prop(name) ⇒ value
prop(name, value) ⇒ self
prop({ name: value, name2: value2, ... }) ⇒ self
读取或设置 DOM properties,比如 checked
和 selected
。属性见 attr
val
val() ⇒ string
val(value) ⇒ self
val(function(index, oldValue){ ... }) ⇒ self
表单元素如input,select,textarea。获取第一个元素的value,或设置所有元素的value。
css
css(property) ⇒ value
css(property, value) ⇒ self
css({ property: value, property2: value2, ... }) ⇒ self
获取集合中第一个元素的css值,或设置所有DOM元素的css值。
var elem = Z('h1')
elem.css('background-color') // 获取css属性
elem.css('background-color', '#369') // 设置css属性
elem.css('background-color', '') // 删除css属性
// 批量设置css属性:
elem.css({ backgroundColor: '#8EE', fontSize: 28 })
hasClass
hasClass(name) ⇒ boolean
判断元素是否有指定参数class。
addClass
addClass(string) ⇒ self
给匹配的元素添加class。
Z('#tab').addClass('hide')
removeClass
removeClass([name]) ⇒ self
删除元素指定 name 的 class。
toggleClass
toggleClass(name) ⇒ self
如果存在值为 name 的 class 移除掉, 反之添加.
replaceClass
replaceClass(oldCls, newCls) ⇒ self
将元素的老 class 替换为新 class.
width
width() ⇒ number
width(value) ⇒ self
width(function(index, oldWidth){ ... }) ⇒ self
读取或设置元素的宽度值(width)。
Z('#foo').width() // => 123
Z(window).width() // => 768 (viewport width)
Z(document).width() // => 768
height
height() ⇒ number
height(value) ⇒ self
height(function(index, oldHeight){ ... }) ⇒ self
读取或设置元素的高度值(height)。
Z('#foo').height() // => 123
Z(window).height() // => 838 (viewport height)
Z(document).height() // => 22302
innerWidth
innerWidth() ⇒ number
获取元素的内宽度,即paddingLeft + width + paddingRight。
innerHeight
innerHeight() ⇒ number
获取元素的内高度,即paddingTop + height + paddingBottm。
outerWidth
outerWidth() ⇒ number
获取元素的外宽度,即borderLeft + paddingLeft + width + paddingRight + borderRight。
outerHeight
outerHeight() ⇒ number
获取元素的外高度,即bordeTop + paddingTop + width + paddingBottom + borderBottom。
marginWidth
marginWidth() ⇒ number
outerWidth再加左右下margin。
marginHeight
marginHeight() ⇒ number
outerHeight再加上上下margin。
position
position() ⇒ object
获取集合中第一个元素的位置,相对于最近定位的父元素。
返回对象具有的属性: top
, left
.
var pos = Z('#ad').position()
// position a tooltip relative to the element
Z('#tooltip').css({
position: 'absolute',
top: pos.top - 30,
left: pos.left
})
offset
offset() ⇒ object
offset(coordinates) ⇒ self
offset(function(index, oldOffset){ ... }) ⇒ self
获取元素相对于文档 (document) 的位置,返回的对象具有属性:
top
, left
.
当传参数coordinates(具有left
和top
属性),将设置元素的top、left。
offsetParent
offsetParent() ⇒ collection
获取最近定位的父元素,也即具有relative,absolute或fixed的父元素。
scrollTop
scrollTop() ⇒ number
scrollTop(top) ⇒ self
获取或设置元素的scrollTop。
show
show() ⇒ self
Restore the default value for the “display” property of each element in the
array, effectively showing them if they were hidden with hide.
hide
hide() ⇒ self
通过css的display隐藏元素。
toggle
toggle([setting]) ⇒ self
元素显示/隐藏切换,如果元素初始时隐藏的,则显示,反之隐藏。
remove
remove() ⇒ self
Remove elements in the current collection from their parent nodes, effectively
detaching them from the DOM.
empty
empty() ⇒ self
清空指定元素的所有子元素.
append
append(content) ⇒ self
在每个匹配元素内的结尾处插入指定的内容。content可以是DOM元素,HTML字符串和 Z
对象自身。
var elem = Z('#nav')
elem.append('hello')
prepend
prepend(content) ⇒ self
在每个匹配元素内的开头插入指定的内容。content可以是DOM元素,HTML字符串和 Z
对象自身。
var elem = Z('#nav')
elem.prepend('hello')
after
after(content) ⇒ self
在每个匹配元素的后面,插入指定的内容,作为其兄弟节点。content可以是DOM元素,HTML字符串和 Z
对象自身。
var elem = Z('#nav')
elem.after('hello')
before
before(content) ⇒ self
在每个匹配元素的前面,插入指定的内容,作为其兄弟节点。content可以是DOM元素,HTML字符串和 Z
对象自身。
var elem = Z('#nav')
elem.before('hello')
事件
on
on(type, function(e){ ... }) ⇒ self
on({type: handler, type2: handler2, ... }) ⇒ self
on(type.name, function(e){ ... }) ⇒ self
给DOM元素绑定事件,可以每次绑定一个事件,也可以批量绑定多个事件,或使用空格间隔添加多个事件。另可以添加具有命名空间的事件。
事件handler执行的时候,事件对象会作为它的第一个参数传入,上下文(this)为DOM元素。
var elem = Z('#content')
elem.on('click', function(e){ ... })
elem.on({click: function(e){ ... }), dblclick: function(e){ ... })
elem.on('mouseover mouseout', function(e){ ... })
elem.on('click.name1', function(e){ ... })
off
off(type, handler) ⇒ self
off({type: handler, type2: handler2, ...}) ⇒ self
off(type) ⇒ self
off() ⇒ self
删除绑定的DOM事件,可以只删除特定的handler,可以批量删除多个事件handler,可以伤处指定事件的所有handler,或者删除元素上的全部hanlder(所有事件类型)
var elem = Z('#content')
elem.off('click', handler})
elem.off({click: handler1, dblclick: handle2})
elem.off('click')
elem.off()
delegate
delegate(selector, type, handler) ⇒ self
事件委托,可为子元素绑定事件。常常用在子元素是动态添加或所有子元素行为一致时。
$('#nav').delegate('li', 'click', function(e){ ... }))
undelegate
undelegate(type, handler) ⇒ self
undelegate(type) ⇒ self
解除事件委托。
$('#nav').undelegate(click', handler)
$('#nav').undelegate(click')
fire
fire(type) ⇒ self
fire(type.name) ⇒ self
主动派发事件,可派发特定命名空间的事件
$('#myDiv').fire(click')
$('#myDiv').fire(click.name1')
one
one(type, function(e){ ... }) ⇒ self
one({ type: handler, type2: handler2, ... }) ⇒ self
给DOM元素添加仅执行一次的事件
var elem = Z('#content')
elem.one('click', function(e){ ... })
elem.one({click: function(e){ ... }), dblclick: function(e){ ... })
elem.one('mouseover mouseout', function(e){ ... })
elem.one('click.name1', function(e){ ... })
delay
delay(type, handler, wait) ⇒ self
给DOM元素绑定一个延迟执行的事件,wait为毫秒。
$('#myDiv').delay(click', function(e){ ... }, 1000)
throttle
throttle(type, handler, wait) ⇒ self
给DOM元素绑定一个事件节流的handler,wait为毫秒。多用在调用频率较高的scroll等事件。
$('#myDiv').throttle(click', function(e){ ... }, 100)
debounce
debounce(type, handler, wait) ⇒ self
给DOM元素绑定一个防止弹跳的handler,wait为毫秒。
$('#myDiv').debounce(click', function(e){ ... }, 100)
immediate
immediate(type, handler, wait) ⇒ self
类似debounce,不同在于其会立即窒息。
$('#myDiv').immediate(click', function(e){ ... }, 100)
Ajax requests
Z.ajax
Z.ajax(options) ⇒ XMLHttpRequest
Z.ajax(url, options) ⇒ XMLHttpRequest
发起一个Ajax请求,返回XMLHttpRequest对象
Options:
-
url
(default: none): 请求的URL
-
async
(default: true): 是否异步请求 true/false
-
method
(default: true): 请求方式 POST or GET
-
type
(default: JSON):
后端返回的数据格式 JSON, XML, HTML, Plain Text
-
data
(default: none):
请求参数,字符串或JSON ( 'a=1&b=2', {a:1, b:2})
-
timeout
(default: 0): 请求超时时间,当超过设定的时间后会放弃该次请求(xhr.abort)
-
credential
(default: false): 跨域请求时是否带证书,为false不带http认证信息,比如cookie
-
scope
(default: window): 成功回调函数的执行上下文
-
success
(default: none): 请求成功回调函数
-
failure
(default: none): 请求失败回调函数
Ajax 回调函数
-
success(res, status, xhr)
: 请求成功回调,参数依次是返回数据、HTTP状态码、XHR对象
-
failure(xhr, errorType)
: 请求失败回调,参数分别为XHR对象,错误类型。
Z.jsonp 🐶🔫
Z.jsonp(options)
Z.jsonp(url, options)
发起一个JSONP跨域请求,获取JSON格式数据。
JSONP使用Script tag发起请求,默认只能GET方式、异步请求、不能处理HTTP头。其参数作为URL一部分传递,当参数较多时需要注意各浏览器对URL长度的限制。
Options:
-
url
(default: none): 请求的URL
-
param
(default: none): 请求参数
-
timestamp
(default: none): 是否加时间戳(避免缓存)
-
success
(default: none):
请求成功回调函数
-
failure
(default: none):
请求失败回调函数
-
jsonpCallback
(default: none):
指定回调函数名称,默认会随机生成函数名。当使用缓存时,timestamp应设为false。
Z.text
Z.text(url, function(res, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.text(url, [data], [function(res, status, xhr){ ... }]) ⇒ XMLHttpRequest
执行一个GET请求,返回纯文本(Plain text)。这是一个Z.ajax的便捷方法。
Z.text('/whatevs.html', function(txt){
Z('#tab').html(txt)
})
Z.json
Z.json(url, function(res, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.json(url, [data], [function(res, status, xhr){ ... }]) ⇒ XMLHttpRequest
执行一个GET请求,返回JSON。这是一个Z.ajax的便捷方法。
Z.json('/add.action', function(obj){
Z('#tab').text(obj.name)
})
Z.xml
Z.xml(url, function(res, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.xml(url, [data], [function(res, status, xhr){ ... }]) ⇒ XMLHttpRequest
执行一个GET请求,返回XML。这是一个Z.ajax的便捷方法。
Z.xml('/add.action', function(xml){
Z('#tab').text(xml.documentElement.getElementsByTagName('AD'))
})
Z.get
Z.get(url, function(res, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.get(url, [data], [function(res, status, xhr){ ... }]) ⇒ XMLHttpRequest
执行一个Ajax GET请求,默认返回JSON。 这是一个Z.ajax的便捷方法。
Z.get('/doXXX.action', function(obj){
Z(document.body).append(obj.name)
})
Z.post
Z.post(url, function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.post(url, [data], function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
执行一个Ajax POST请求,默认返回JSON。 这是一个Z.ajax的便捷方法。
Z.post('/awesome.json', function(json){
console.log(json)
})
Z.sync
Z.sync(url, function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.sync(url, [data], function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
执行一个Ajax同步请求,默认返回JSON。 这是一个Z.ajax的便捷方法。
Z.sync('/awesome.json', function(json){
console.log(json)
})
Z.async
Z.async(url, function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
Z.async(url, [data], function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
执行一个Ajax异步请求,默认返回JSON。 这是一个Z.ajax的便捷方法。
Z.async('/awesome.json', function(json){
console.log(json)
})
Z.param
Z.param(object, [shallow]) ⇒ string
Z.param(array) ⇒ string
序列号一个对象为HTTP URL需要的查询参数,如设置了shallow为true,则会递归序列化。
Z.param({ foo: { one: 1, two: 2 }})
//=> "foo[one]=1&foo[two]=2)"
Z.param({ ids: [1,2,3] })
//=> "ids[]=1&ids[]=2&ids[]=3"
Z.param({ ids: [1,2,3] }, true)
//=> "ids=1&ids=2&ids=3"
Z.param({ foo: 'bar', nested: { will: 'not be ignored' }})
//=> "foo=bar&nested[will]=not+be+ignored"
Z.param({ foo: 'bar', nested: { will: 'be ignored' }}, true)
//=> "foo=bar&nested=[object+Object]"
面向对象
Z.Class
Z.Class(name, factory) ⇒ function
Z.Class(name, [superClass], factory) ⇒ function
定义一个类,默认定义的window上。name为类名,字符串类型;superClass为父类,function类型;factory为构造函数,function类型。
定义一个简单类
Z.Class('Person', function() {
this.init = function(name, age) {
this.name = name
this.age = age
}
this.log = function() {
alert('name: ' + this.name + ', age: ' + this.age)
}
})
var p = new Person('John McCarthy', 80)
p.log() // 'name: John McCarthy, age: 80'
私有属性、方法
Z.Class('Person', function() {
var privateVar = []
var helpMethod = function() { ... }
this.init = function(name, age) {
...
}
})
定义一个命名空间类
Z.Class('Z.ui.Dialog', function() {
this.init = function(options) {
...
}
this.open = function() {
...
}
})
var dialog = new Z.ui.Dialog(options)
dialog.open()
指定顶级命名空间
var Blog = {}
Z.Class.globalNamespace = Blog
Z.Class('ui.Dialog', function() {
this.init = function(name, age) {
...
}
})
var dialog = new Blog.ui.Dialog()
继承
Z.Class('Z.ui.Dialog', Z.ui.Window, function(supr) {
this.init = function(options) {
supr.init.call(options)
...
}
this.show = function() {
...
}
})
Z.statics
Z.statics(clazz, obj) ⇒ function
给类添加静态属性、方法。
Class.statics(Person, {
prop: 'xx',
method: function() { ... }
})
Person.prop // -》'xx'
Person.method() // call static method
Z.methods
Z.methods(clazz, obj [,override]) ⇒ function
给类添加属性、方法。实际是添加到类的prototype上。override为true表示将覆盖原有的属性、方法。
Class.methods(Person, {
prop: 'xx',
method: function() { ... }
})
var p = new Person()
p.prop // -》'xx'
p.method() // call instance's method
Z.agument
Z.agument(clazz, clazz1, class2, [...]) ⇒ function
Z.agument(override, clazz, class1, clazz2, [...]) ⇒ function
掺合,多继承,override为true表示将覆盖原有的属性、方法。clazz1/clazz2为function类型时只继承其原型上的属性,方法。
从对象掺合
Class.agument(Z.ui.Dialog, {show: function() {}}, {hide: function() {}})
从多个类继承
Class.agument(Z.ui.Dialog, Z.ui.Component, Z.ui.window)
对象相关
Z.Objects.keys
Z.Objects.keys(obj) ⇒ array
提取对象的key,返回存放key的数组。
Z.Object.keys({one:1, two:2, three:3});
=> ['one', 'two', 'three']
Z.Objects.values
Z.Objects.values(obj) ⇒ array
提取对象的value,返回存放value的数组。
Z.Object.keys({one:1, two:2, three:3});
=> [1, 2, 3]
Z.Objects.pairs
Z.Objects.pairs(obj) ⇒ array
对象转成数组,数组第一个元素存放key,第二个存放value。
Z.Object.pairs({one:1, two:2, three:3});
=> [['one', 1], ['two', 2], ['three', 3]]
Z.Objects.invert
Z.Objects.invert(obj) ⇒ object
把对象的key和value互换后返回。
Z.Object.invert({one:1, two:2, three:3});
=> {'1': 'one', '2': 'two', '3': 'three'}
Z.Objects.pick
Z.Objects.pick(obj, *keys) ⇒ object
抽取指定的key的对象后返回。
Z.Object.pick({one:1, two:2, three:3}, 'one', 'three');
=> {one: 1, three: 3}
数组
Z.Array.forEach
Z.Array.forEach(obj, function(){ ... }, context)
Z.Array.map
Z.Array.map(obj, function(){ ... }, context)
Z.Array.every
Z.Array.every(obj, function(){ ... }, context)
Z.Array.some
Z.Array.some(obj, function(){ ... }, context)
Z.Array.contains
Z.Array.contains(obj, item) ⇒ boolean
Z.Array.filter
Z.Array.filter(obj, function(){ ... }, context)
Z.Array.unique
Z.Array.unique(obj, function(){ ... }, context)
函数相关
Z.Function.bind
Z.Function.bind(func, context) ⇒ function
绑定函数在指定上下文对象上执行。
Z.Function.once
Z.Function.once(func) ⇒ function
返回一个新函数,该函数仅会执行一次。
Z.Function.uniq
Z.Function.uniq(func, wait) ⇒ function
指定的函数只有达到指定的时间才会执行,比如提交订单时防止用户点击过快。
Z.Function.delay
Z.Function.delay(func, wait) ⇒ function
延迟一定时间函数才执行。
Z.Function.debounce
Z.Function.debounce(func, wait, immediate) ⇒ function
于uniq类似,多用在scroll等时间中。
Z.Function.throttle
Z.Function.throttle(func, wait) ⇒ function
事件节流,一些频繁调用的事件如scroll、resize。
字符串
escape
Z.String.escape(string) ⇒ string
把特殊字符还原成HTML,&, <, >, ', /会被替换。
Z.String.escape('John, Larry & Backus');
=> 'John, Larry & Backus'
unescape
Z.String.unescape(string) ⇒ string
把HTML转义,&, <, >, ", ', /会被替换。
Z.String.unescape('John, Larry & Backus');
=> 'John, Larry & Backus'
urlAppend
Z.String.urlAppend(url, str) ⇒ string
给URL后添加一个参数
Z.String.urlAppend('http://some.abc.com', 't=20140312');
=> 'http://some.abc.com?t=20140312'
trim
Z.String.trim(string) ⇒ string
去掉字符串两边的空白字符串
Z.String.trim(' Hi ');
=> 'Hi'
isNumberStr
Z.String.isNumberStr(string) ⇒ boolean
判断是否都是数字的字符串
toInt
Z.String.toInt(string) ⇒ number
转换成数字
toFloat
Z.String.toFloat(string) ⇒ number
转换成浮点数字
ellipsis
Z.String.ellipsis(str, len, word) ⇒ string
省略多余的字符,以'...'代替
Z.String.ellipsis('Hello, John Backus.', 15);
=> 'Hello, John...'
stripScript
Z.String.stripScript(str) ⇒ string
去掉字符串里的script标签
format
Z.String.format(str, x1, x2, ...) ⇒ string
简易模板替换
Z.String.format('Hello, {0}, {1}!', 'Jack', 'how are you');
=> 'Hello, Jack, how are you!'
类型判断
Z.isArray
Z.isArray(object) ⇒ boolean
判断是否为数组类型
Z.isArray([1,2]) // => true
Z.isArray({}) // => false
Z.isFunction
Z.isFunction(object) ⇒ boolean
判断是否为函数类型
Z.isFunction([].push) // => true
Z.isFunction({}) // => false
Z.isObject
Z.isObject(object) ⇒ boolean
判断是否为对象类型
Z.isObject({}) // => true
Z.isObject([]) // => false
Z.isString
Z.isString(object) ⇒ boolean
判断是否为字符串类型
Z.isString('hello') // => true
Z.isString(10) // => false
Z.isNumber
Z.isNumber(object) ⇒ boolean
判断是否为字符串类型
Z.isNumber(10) // => true
Z.isNumber('hello') // => false
Z.isBoolean
Z.isBoolean(object) ⇒ boolean
判断是否为布尔类型
Z.isBoolean(false) // => true
Z.isBoolean({}) // => false
Z.isPrimitive
Z.isPrimitive(object) ⇒ boolean
判断是否为基本类型,基本类型包括String、Number、Boolean、undefined、null。
Z.isPrimitive(false) // => true
Z.isPrimitive(10) // => true
Z.isPrimitive('hello') // => true
Z.isPrimitive(undefined) // => true
Z.isPrimitive(null) // => true
Z.isPrimitive({}) // => false
Z.isPrimitive([]) // => false
Z.isEmptyObject
Z.isEmptyObject(object) ⇒ boolean
判断是否为一个空的对象
Z.isEmptyObject({}) // => true
Z.isEmptyObject({name: 'Backus'}) // => false
Z.isArrayLike
Z.isArrayLike(object) ⇒ boolean
判断是否为一个类数组(ArrayLike)
Z.isArrayLike(NodeList) // => true
Z.isArrayLike(Collection) // => true
Z.isArrayLike({'1': 'one', '2': 'two', length: 2}) // => true
Z.isArrayLike({}) // => false
Z.isWindow
Z.isWindow(object) ⇒ boolean
判断是否为window对象
Z.isWindow(window) // => true
Z.isWindow(document) // => false
Z.isDocument
Z.isDocument(object) ⇒ boolean
判断是否为document对象
Z.isDocument(document) // => true
Z.isDocument(window) // => false
Z.isElement
Z.isElement(object) ⇒ boolean
判断是否为HTMLElement对象
Z.isElement(Z('div')[0]) // => true
Z.isElement(window) // => false
Z.isTextNode
Z.isTextNode(object) ⇒ boolean
判断是否为HTMLTextNode对象,即文本对象
Z.isTextNode(Z('div').text()) // => true
isURL
Z.String.isURL(string) ⇒ boolean
判断是否是有效的URL
isEmail
Z.String.isEmail(string) ⇒ boolean
判断是否是有效的email
Z.isZ
Z.isTextNode(object) ⇒ boolean
判断是否为 Z
对象
Z.isZ(Z('div')) // => true
浏览器判断
// specific browser
Z.chrome
Z.firefox
Z.safari
Z.opera
Z.ie
Z.ie6
Z.ie7
Z.ie8
Z.ie9
Z.ie10
Z.maxthon
Z.tt
鸣谢
This documentation is based on the layout of the
Backbone.js
documentation, which is released under the
MIT license.
Z.js and this documentation is released under the terms of the MIT license.