When registering a click event for a, there is a default behavior, the way to prevent the default behavior: retrun false
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="">Click to print in console hahaha</a> <script> // 1. Get a var link = document.getElementsByTagName('a')[0 ]; // 2. Register a click event for a link.onclick = function (){ console.log( 'Hahaha' ); console.log(1); return false ; // To prevent the default behavior of the a tag, write return false in the last line of the event handler } </script> </body> </html>