我正在尝试创建一个简单的计算器,当单击一个按钮时,它的值显示在文本字段中,按钮“C”应该清除文本字段但是它的onclick =“clear()”不起作用?
<%@page contentType="text/html" pageEncoding="UTF-8"%> Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> culator
with
Use of the with statement is not recommended,as it may be the source of confusing bugs and compatibility issues.
因此,您实际上是在调用document.clear()而不是全局clear().
document.clear()
对此的快速解决方法是将函数重命名为其他内容或显式调用window.clear().
更好的解决方案是使用addEventListener而不是内部事件属性绑定事件处理程序.
addEventListener