首页 > 解决方案 > 进行函数调用时,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不起作用。我不明白为什么这不起作用。

标签: htmlbutton

解决方案


这是一个挑剔的。

这里的问题是这个词cleardocument.clear. 因此,正在请求该方法而不是您的方法。

clear解决方案是使用与运行该功能不同的词。

另一种解决方案是不使用内联事件处理程序,而是使用 javascript 在代码中定义侦听器。

这里有一些关于document.clear(). 它正在从 Web 标准中删除。

https://developer.mozilla.org/en-US/docs/Web/API/Document/clear


推荐阅读