单击外部链接以及如何将其添加到链接类时发出警告 [英] Warning when clicking external links and how to add it to a link class

查看:40
本文介绍了单击外部链接以及如何将其添加到链接类时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何使用javascript在单击外部链接时发出警告警告的弹出窗口.

I'm not sure how to do a pop-up that warns when you are clicking on external links, using javascript.

我认为在我的外部链接上放一个类也很方便,但是我不确定现在是否正确.这是我目前正在使用的HTML:

I figured that it would be handy to put a class on my external links as well, but I'm not quite sure it's done correct as it is now either. This is the HTML I'm using at the moment:

        <div id="commercial-container">
        <a href="http://www.link1.com" class="external" target="_blank"> <img src="picture1.jpg" /> </a>
        <a href="http://www.link2.com" class="external" target="_blank"> <img src="pciture2.jpg" /> </a>
        <a href="http://www.link3.com" class="external" target="_blank"> <img src="picture3.jpg" /> </a>
        <a href="http://www.link4.com" class="external" target="_blank"> <img src="picture4" /> </a>
        </div>

我对javascript很陌生,对如何解决我的问题也不确定.到目前为止,我几乎唯一想到的就是我将不得不使用window.onbeforeload,但是我不知道如何弄清楚如何编写所需的函数.

I'm very new to javascript and very unsure on how to solve my problems. The pretty much only thing I figured out so far is that I will have to use window.onbeforeload but I have no clue on how to figure out how to write the function I need.

我想将我的JavaScript保留在单独的.js文档中,而不是保留在HTML中.

I want to keep my javascript in a separated .js document instead of in the HTML as well.

推荐答案

从onClick属性调用confirm()函数.如果用户单击确定",此函数将返回true,这将打开链接,否则将返回false.

Call the confirm() function from the onClick attribute. This function returns true if the user clicks OK, which will open the link, otherwise it will return false.

<a href="http://www.link1.com" onClick="return confirm('Do you want to leave')" class="external" target="_blank"> <img src="picture1.jpg"/> </a>

希望这会有所帮助.

这篇关于单击外部链接以及如何将其添加到链接类时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆