js Tutorial Series 35: javascript-DOM: Register a click event for a to prevent the default behavior of the a tag

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>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808688&siteId=291194637