Javascript 如何在锚标签 <a href> 中打开警告框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29694604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a alert box within anchor tag <a href>?
提问by Anuja Lamahewa
I want to open a alert box when a certain link is clicked. How to do that ?
我想在单击某个链接时打开一个警报框。怎么做 ?
回答by
You can also use the following codes:
您还可以使用以下代码:
<a href="#" id="link">Link</a>
Javascript:
Javascript:
$(document).on("click","#link",function(){
alert("I am a pop up ! ");
});
OR:
或者:
<a href="#" onclick="alert('I am a popup!');">Link</a>
回答by Shuma
Without using a JS file (purely as an alternative to answers already posted):
不使用 JS 文件(纯粹作为已经发布的答案的替代):
<a href="http://example.com" onclick="alert('Hello world!')">Link Text</a>
回答by Anuja Lamahewa
<a href="#" onclick="myFunction(); return false;">
The return false;will stop the web page being loaded after clicking.
该return false;会停止点击后正在加载的网页。
JavaScript code:
JavaScript 代码:
function myFunction() {
alert("I am a pop up ! ");
}
回答by Varun
The following code might help:-
以下代码可能会有所帮助:-
<a id="anchor">i am the anchor</a>
<div id="popup">this is a popup</div>
<script>
$(function(){
$('#popup').hide();
$('#anchor').click(function(){
$('#popup').show();
})
});
</script>

