Today I saw the two parameter passing forms of this and event in the chapter "Introduction to JavaScript - Events". Because as a junior front-end developer, I have only used this to pass parameters, so I really want to figure out what is the difference between this and event, and which one is more appropriate to use under what circumstances.
onclick = changeImg(this) vs onclick = changeImg(event)
##
<img src='usa.gif' onclick="changeImg(event)" /> <script> var myImages = [ 'usa.gif','canada.gif','jamaica.gif','mexico.gif' ]; function changeImg(e) { var el = e.target; var newImgNumber = Math.round(Math.round()*3); while(el.src.indexOf(myImages[newImgNumber]) != -1){ el.src =myImages[newImgNumber]; } } </script>
The above is the detailed content of Detailed explanation of the difference between this and event in JS. For more information, please follow other related articles on the PHP Chinese website!