Để làm điều này, sử dụng preventDefault() trong JavaScript. Dưới đây là mã JavaScript:
Ví dụ
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <a href="urlLink">Press Me to see logs</a> <a href="urlLink">Press Me to see logs in console</a> <a href="#">Nothing will happen</a> <script> $(function(){ $("a:not([href='#'])").click(function(event){ event.preventDefault(); console.log("You have pressed me!!!!!"); }); }); </script> </body> </html>Để chạy chương trình trên, lưu tệp với tên anyName.html (index.html) và chuột phải vào tệp. Chọn tùy chọn Open with live server trong trình soạn thảo VS code.
Kết quả

Nếu bạn nhấn vào hai liên kết Press Me to see logs và Press Me to see logs in console, bạn sẽ nhận được đầu ra console. Tuy nhiên, nếu bạn nhấn vào liên kết Nothing will happen, nó sẽ không in ra gì:

Ở trên, tôi đã nhấn Press Me to see logs để hiển thị đầu ra.
Nếu bạn nhấn vào liên kết Nothing will happen, nó sẽ không in ra gì như đầu ra dưới đây:
