I want to remove the clicked element from the page after a certain amount of time (1.5 seconds). Here is the code (including some background info):
function AttachEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + type, handler)
} else {
element['on' + type] = handler;
}
}
AttachEvent(window, "load", function() {
AttachEvent(mydiv, "click", do_stuff);
});
function do_stuff(e){
e = e || window.event;
var target = e.target || e.srcElement;
//some stuff
//remove object
setTimeout('target.parentNode.removeChild(element);', 1500);
}
Internet Explorer complains about target being undefined in the anonymous function. How do I set this timeout in Internet Explorer?
Don't use a string for setTimeout
. Just don't . Instead pass an anonymous function ( demo ):
function do_stuff(e){
e = e || window.event;
var target = e.target || e.srcElement;
//some stuff
//remove object
setTimeout(function(){target.parentNode.removeChild(target);}, 1500);
}
If you use the function above, the current value of target
will be used inside of the anonymous function. If you pass a string, your browser looks for a global object named target
, which will fail, since target a function scope variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.