Onclick function “is not defined”

前端 未结 4 550
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 20:08

I am fairly new to wordpress and am trying to define and call a function but cannot get it to work.

The following code appears in a php file that is called from the

4条回答
  •  一向
    一向 (楼主)
    2020-12-28 20:21

    Using OnClick or similar attributes is very poor practice.

    Using Javascript Event Listener is a much better way of doing this. Registering an event in a modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

    HTML:

    Your JavaScript is supposed to look like this:

    document.getElementById ("checkAllTopicCheckBoxes").addEventListener ("click", myFunction, false);
    
    function myFunction() {
      alert("Hello! I am an alert box!!");
    }
    

    HTML attribute (such as Onclick) should be avoided as it concerns the content/structure and behavior are not well-separated, making a bug harder to find.

提交回复
热议问题