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>根据 DOM 规范,全局事件处理程序环境在事件处理期间隐藏元素的范围。因此,元素的 'animate' 方法优先于全局 'animate' 函数。
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>要纠正这个问题,可以采用多种方法:
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')));
以上是为什么我的'animate”函数在 IE 中工作但在 Chrome 中不起作用:JavaScript 阴影问题?的详细内容。更多信息请关注PHP中文网其他相关文章!