html - 进行函数调用时,onclick 函数不起作用
问题描述
我有一个按钮在单击按钮时不会调用该函数。该onclick
函数可以显示警报窗口,但不运行函数调用。
...
<fieldset>
<legend>Billing Information</legend>
<label for="name2">Name:</label><br>
<input type="text" id="name2" name="full_name"><br>
<label for="zip2">Zip code:</label><br>
<input type="text" id="zip2" name="zip_code">
</fieldset>
<button type="button" onclick="clear()">Verify</button>
...
<script>
function clear() {
document.getElementById("name1").value = "";
document.getElementById("zip1").value = "";
document.getElementById("name2").value = "";
document.getElementById("zip2").value = "";
}
...
</script>
所以这有效:
<button type="button" onclick="alert("Hello")">Verify</button>
但该功能clear
不起作用。我不明白为什么这不起作用。
解决方案
这是一个挑剔的。
这里的问题是这个词clear
在document.clear
. 因此,正在请求该方法而不是您的方法。
clear
解决方案是使用与运行该功能不同的词。
另一种解决方案是不使用内联事件处理程序,而是使用 javascript 在代码中定义侦听器。
这里有一些关于document.clear()
. 它正在从 Web 标准中删除。
https://developer.mozilla.org/en-US/docs/Web/API/Document/clear
推荐阅读
- javascript - 定位键:对象的值
- emv - 启用 EMV 芯片的卡是否可以不生成 ARQC 密码?
- router - Full Cone NAT 路由器能否自动打开由使用对称 NAT 的路由器发起的动态端口?
- maven - com.spotify/dockerfile-maven-plugin:标记失败:路径为“空”
- apache - 如何使用 glassfish 服务器修复 'AH00992: ajp_read_header: ajp_ilink_receive failed' apache 代理错误?
- jmeter - 在 NTLM 身份验证后,Jmeter HTTP 请求保持循环重定向
- solr - 无法启动 solr
- html - 如何阻止我的视差 div 背景模糊?
- node.js - 并行的函数永远不会在 Gulp 中执行
- flutter - Flutter 同名小部件类意外共享同一个实例