javascript – 为什么我不能从onclick属性中调用名为clear的函数?

我正在尝试创建一个简单的计算器,当单击一个按钮时,它的值显示在文本字段中,按钮“C”应该清除文本字段但是它的onclick =“clear()”不起作用?

<%@page contentType="text/html" pageEncoding="UTF-8"%>
Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        culator
最佳答案
内在事件属性(如onclick)非常糟糕.在内部,他们实施with

Use of the with statement is not recommended,as it may be the source of confusing bugs and compatibility issues.

因此,您实际上是在调用document.clear()而不是全局clear().

对此的快速解决方法是将函数重命名为其他内容或显式调用window.clear().

更好的解决方案是使用addEventListener而不是内部事件属性绑定事件处理程序.

enter image description here

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些