JS 函数“Animate”在 Chrome 中失败,但在 IE 中成功:解决遮蔽问题
问题描述
带有内联函数的 onClick 事件旨在改变元素样式的名为“animate”的函数在 Chrome 中失败,但在 IE 中按预期运行。 “animate”函数中调用的代码旨在使用 JavaScript 修改元素的位置和颜色。
function animate() { var div = document.getElementById('demo'); div.style.left = "200px"; div.style.color = "red"; }
#demo { position: absolute; }
<p></p>
解决方案和说明
问题在于 JavaScript 的范围链和影子机制。当使用内联事件处理程序属性(例如 onclick)时,全局函数“animate”会被最近引入的“Element.prototype.animate”方法所掩盖,该方法源于 Web 动画 API。
window.animate // global function Element.prototype.animate // element's own method
根据 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).
要纠正这个问题,可以采用多种方法:
- 重命名全局函数:
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中文网其他相关文章!

ThebestmethodforincludingCSSdependsonprojectsizeandcomplexity:1)Forlargerprojects,useexternalCSSforbettermaintainabilityandperformance.2)Forsmallerprojects,internalCSSissuitabletoavoidextraHTTPrequests.Alwaysconsidermaintainabilityandperformancewhenc

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

我知道,我知道:有大量的内容管理系统选项可用,而我进行了几个测试,但实际上没有一个是一个,y&#039;知道吗?怪异的定价模型,艰难的自定义,有些甚至最终成为整个&

链接CSS文件到HTML可以通过在HTML的部分使用元素实现。1)使用标签链接本地CSS文件。2)多个CSS文件可通过添加多个标签实现。3)外部CSS文件使用绝对URL链接,如。4)确保正确使用文件路径和CSS文件加载顺序,优化性能可使用CSS预处理器合并文件。

选择Flexbox还是Grid取决于布局需求:1)Flexbox适用于一维布局,如导航栏;2)Grid适合二维布局,如杂志式布局。两者在项目中可结合使用,提升布局效果。

包含CSS文件的最佳方法是使用标签在HTML的部分引入外部CSS文件。1.使用标签引入外部CSS文件,如。2.对于小型调整,可以使用内联CSS,但应谨慎使用。3.大型项目可使用CSS预处理器如Sass或Less,通过@import导入其他CSS文件。4.为了性能,应合并CSS文件并使用CDN,同时使用工具如CSSNano进行压缩。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版
好用的JavaScript开发工具

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。