tools


#coffeescript
isIE = /msie/i.test(navigator.userAgent)
traverseChildren = (elem)->
pushAll = (elemArray) ->
q.push item for item in elemArray
children = [];
q = [];
q.push(elem);
while q.length > 0
elem = q.pop();
children.push(elem);
pushAll(elem.children);
return children;
mouseoutEvent = (event,dom,callback)->
elem = event.toElement || event.relatedTarget
list = traverseChildren(dom)
if elem in list
return;
callback()
return


appendEvent = (dom, event, fun) ->
if isIE
dom.attachEvent("on" + event, fun)
else
dom.addEventListener(event, fun, false)
return

hasClass = (ele,cls)->
if not ele
return
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'))
addClass = (ele,cls)->
if not ele
return
if not hasClass(ele,cls) then ele.className += " "+cls
return
removeClass = (ele,cls)->
if not ele
return
if hasClass(ele,cls)
reg = new RegExp('(\\s|^)'+cls+'(\\s|$)')
ele.className=ele.className.replace(reg,' ')
return
getDomIndex = (e)->
i=0
while e.previousSibling
e=e.previousSibling
if(e.nodeType is 1)
i=i+1
return i

jsonp = (url)->
_href = window.location.href
if _href.indexOf('https://') is 0
url = url.replace('http://','https://')
methodName = 'jsonp' + Math.random().toString(36).substring(2)
data = null
callbackChain=[]

window[methodName] = (callbackData)->
data=callbackData
for m in callbackChain
m(data)
return
url=url.replace('callback=?','callback='+methodName)
script = document.createElement( 'script' )
script.setAttribute( 'src', url )
script.setAttribute( 'charset', "utf-8" )
document.getElementsByTagName( 'head' )[0].appendChild(script)
return {
done:(fun)->
if(data)
fun(data)
callbackChain.push(fun)
return
}

window.isIE = isIE
window.traverseChildren = traverseChildren
window.mouseoutEvent = mouseoutEvent
window.appendEvent = appendEvent
window.hasClass = hasClass
window.addClass = addClass
window.removeClass = removeClass
window.getDomIndex = getDomIndex
window.jsonp = jsonp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值