JavaScript 阻止超链接跳转的操作方法(多种写法)
JavaScript 阻止超链接跳转的操作方法多种写法通过JavaScript,我们可以阻止超链接的跳转。
方法如下:
(1)操作超链接的 href 属性
写法一:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <a href="javascript:void(0);" rel="external nofollow" >超链接</a> </body> </html>
写法二:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <a href="javascript:;" rel="external nofollow" >超链接</a> </body> </html>
点击链接,链接并不会进行跳转。
(2)阻止链接的默认行为
写法一:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <a href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" >百度</a> <script> var link = document.querySelector("a"); link.addEventListener('click',function(e){ e.preventDefault(); }) </script> </body> </html>
写法二:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <a href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" >百度</a> <script> var link = document.querySelector("a"); link.onclick = function (e) { return false; } </script> </body> </html>
此时,点击超链接,也不会进行跳转。
以上就是JavaScript 阻止超链接跳转的操作方法(多种写法)的详细内容,更多关于js超链接跳转的资料请关注开心学习网其它相关文章!