Home >Web Front-end >CSS Tutorial >Why Does My `animate` Function Work in IE But Not Chrome: A JavaScript Shadowing Issue?
function animate() { var div = document.getElementById('demo'); div.style.left = "200px"; div.style.color = "red"; }
#demo { position: absolute; }
<p>
window.animate // global function Element.prototype.animate // element's own method<p>According to the DOM specifications, the global event handler environment shadows the element's scope during event handling. Thus, the 'animate' method of the element takes precedence over the global 'animate' function.
10. Let the current event handler value be the result of the following steps: ... 4. Let the target environment be the lexical environment scope. ... 6. If the element is non-null, then a. Let the target environment be the result of NewObjectEnvironment(the element, the target environment).<p>To rectify this, one can employ several approaches:
function animate__() { var div = document.getElementById('demo'); div.style.left = "200px"; div.style.color = "red"; }
document.getElementById('demo').addEventListener('click', animate.bind(document.getElementById('demo')));
The above is the detailed content of Why Does My `animate` Function Work in IE But Not Chrome: A JavaScript Shadowing Issue?. For more information, please follow other related articles on the PHP Chinese website!