亲宝软件园·资讯

展开

js超链接跳转 JavaScript 阻止超链接跳转的操作方法(多种写法)

乱舞春秋__ 人气:0
想了解JavaScript 阻止超链接跳转的操作方法(多种写法)的相关内容吗,乱舞春秋__在本文为您仔细讲解js超链接跳转的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:js超链接跳转,js阻止超链接跳转,下面大家一起来学习吧。

通过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>

此时,点击超链接,也不会进行跳转。

加载全部内容

相关教程
猜你喜欢
用户评论