1. 程式人生 > 程式設計 >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="
java
script:void(0);" rel="external nofollow" >超連結</a> </body> </html>

寫法二:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"http://www.cppcns.com 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>

JavaScript 阻止超連結跳轉的操作方法(多種寫法)

點選連結OeFnCjg,連結並不會進行跳轉。

(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" cohttp://www.cppcns.comntent="width=device-width,initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<a href="https://www.baidu.com" rel="external nofollow程式設計客棧"  rel="external nofollo程式設計客棧w" >百度</a>
	<script>
		var link = document.querySelector("a");
		link.onclick = function (e) {
			return false;
		}
	</script>
</body>
</html>

JavaScript 阻止超連結跳轉的操作方法(多種寫法)

此時,點選超連結,也不會進行跳轉。

以上就是JavaScript 阻止超連結跳轉的操作方法(多種寫法)的詳細內容,更多關於js超連結跳轉的資料請關注我們其它相關文章!