按钮onclick不会调用javascript函数

Abh*_*bhi 0 javascript

我在点击按钮时调用一个函数.但是这个功能没有被调用.我不知道我做错了什么.请帮忙.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear() {
      document.getElementById('inp').value = "";
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Raj*_*esh 5

clear不是保留字,而是它的呼唤document.clear.尝试更新功能名称

样品

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear1() {
      document.getElementById('inp').value = "";
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear1()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

示例 - 覆盖document.clear

注意:覆盖document/window/prototype函数是一种不好的做法.这仅用于演示目的.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear1() {
      document.getElementById('inp').value = "";
    }
    document.clear = function(){
      console.log('My clear function')
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

参考

"明确"是Javascript中的保留字吗?