如何使用提醒来阻止链接转到该页面?我一直想弄明白,但它还是把我送到了链接
<!DOCTYPE HTML>
<html>
<head>
<title>Do Not Click</title>
<meta charset="UTF-8" />
</head>
<body>
<script type="text/javascript">
function confirm_alert(node) {
return confirm("STOP CLICKING THE LINK");
}
</script>
<a href="www.yahoo.com" onclick="return confirm_alert(this);">Don't click link</a>
</body>
</html>
答案 0 :(得分:1)
只需从confirm_alert函数返回false。
<!DOCTYPE HTML>
<html>
<head>
<title>Do Not Click</title>
<meta charset="UTF-8" />
</head>
<body>
<script type="text/javascript">
function confirm_alert(node) {
confirm("STOP CLICKING THE LINK");
return false;
}
</script>
<a href="www.yahoo.com" onclick="return confirm_alert(this);">Don't click link</a>
</body>
</html>
答案 1 :(得分:0)
您可以传递参数event
并执行函数.preventDefault()
<!DOCTYPE HTML>
<html>
<head>
<title>Do Not Click</title>
<meta charset="UTF-8" />
</head>
<body>
<script type="text/javascript">
function confirm_alert(node, e) {
e.preventDefault();
return confirm("STOP CLICKING THE LINK");
}
</script>
<a href="www.yahoo.com" onclick="return confirm_alert(this, event);">Don't click link</a>
</body>
</html>
更好的方法是绑定事件点击:
document.querySelector('a').addEventListener('click', confirm_alert);
function confirm_alert(e) {
e.preventDefault();
return confirm("STOP CLICKING THE LINK");
}
<a href="www.yahoo.com">Don't click link</a>
答案 2 :(得分:0)