收集一些常用的代码
 

一次性事件

类似jQuery中的one方法:

// create a one-time event

functiononetime(node, type, callback) {

 

    // create event

    node.addEventListener(type, function(e) {

        // remove event

        e.target.removeEventListener(e.type, arguments.callee);

        // call handler

        returncallback(e);

    });

 

}


调用:

// one-time event

onetime(document.getElementById("myelement"), "click", handler);

 

// handler function

functionhandler(e) {

    alert("You'll only see this once!");

}

摘自这里

2013-10-28 /
标签: javascript
 
评论
© 代码|Powered by LOFTER