首页 > 解决方案 > Q. javascript. why don't play 'onclick'?

问题描述

why don't play onclick="catch()" ? I don't understand.

<script>
  function catch () {
    alert("safsf");
  }
</script>
...

<body>
  <td><img src="media/strawberry.png" onclick="catch()"></td>
</body>

标签: javascriptdom-events

解决方案


catch is a reserved keyword in javascript. Therefore we can't use it in our function/variable names. Try changing the name of the function:

function error(){
  alert(2);
}
<div onclick="error()" class="test">click me 2</div>


推荐阅读