document.getElementById(
"myelement"
).addEventListener(
"click"
, handler);
// handler function
function
handler(e) {
// remove this handler
e.target.removeEventListener(e.type, arguments.callee);
alert(
"You'll only see this once!"
);
}
封装之后的版本
function
onetime(node, type, callback) {
// create event
node.addEventListener(type,
function
(e) {
// remove event
e.target.removeEventListener(e.type, arguments.callee);
// call handler
return
callback(e);
});
}
使用
onetime(document.getElementById(
"myelement"
),
"click"
, handler);
function
handler(e) {
alert(
"You'll only see this once!"
);
}