add an onclick event to a div

后端 未结 3 1005
长发绾君心
长发绾君心 2020-12-16 09:54

I don\'t know if this is allowed or even possible. Is it possible to create a div element with an onclick event so that if anywhere in the div\'s area is clicked, the event

3条回答
  •  Happy的楠姐
    2020-12-16 10:37

    Assign the onclick like this:

    divTag.onclick = printWorking;
    

    The onclick property will not take a string when assigned. Instead, it takes a function reference (in this case, printWorking).
    The onclick attribute can be a string when assigned in HTML, e.g.

    , but this is generally not recommended.

提交回复
热议问题