本文介绍如何使用JavaScript在用户点击链接时触发确认提醒。通过监听点击事件,判断目标是否为链接,并弹出确认对话框。若用户确认,则执行跳转或下载;若取消,则阻止默认行为。适用于需要防止误操作或提示下载的场景。
实现链接点击确认与下载提示
<html>
<head>
<style type="text/css">
html,body{ font-size:12px;}
a {color:#4d5d2c;text-decoration:underline;cursor:pointer;}
.pointer {cursor:pointer;}
.infobar {background:#fff9e3;border:1px solid #fadc80;color:#743e04;margin-bottom:10px;padding:8px 20px 4px 20px;}
</style>
</head>
<body>
<div id="downloadPanel" style="position:absolute;top:0px;left:0px;width:160px;z-index:999;padding:6px 2px 6px 10px;border:1px solid #fb7;display:none;" class="infobar">
<div style="padding:3px 0 0 0;">打开网址 复制 关闭</div>
</div>
<div id="xx">
http://www.baidu.com
https://www.jb51.net
http://www.163.com
http://www.sina.com
http://www.qq.com
</div>
</body>
</html>
注意:引入外部JavaScript代码后,需刷新页面方可生效。