javascript 单击按钮时显示警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25214647/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:10:25  来源:igfitidea点击:

Show alert on button click

javascriptjqueryalert

提问by Mike White

I'm sure this will turn out to be very simple, I got a piece of code that doesn't seem to be loading properly. This is the code I have on my Simple php page

我相信这会变得非常简单,我得到了一段似乎没有正确加载的代码。这是我在 Simple php 页面上的代码

<script language="JavaScript" type="text/javascript">
    function exit_alert(){
        alert("Go Install the XX Antivirus now\n\nto prevent more issues!");
    }
</script>

The user clicks the only button on the page, as soon as he does it, an exit alert should pop with a message like "Go Install the XX Antivirus now to prevent more issues". The exit alert should be a message only, and should only have an OK button on the bottom, and not ask the user Stay or leave.

用户点击页面上唯一的按钮,一旦点击,就会弹出退出警报,并显示“立即安装 XX 防病毒软件以防止更多问题”之类的消息。退出警报应该只是一条消息,并且应该只在底部有一个 OK 按钮,而不是询问用户是留下还是离开。

I've tried lot of stuff but nothing seems to be working, I could really use your help :)

我尝试了很多东西,但似乎没有任何效果,我真的可以使用您的帮助:)

Big thanks in Advance!

非常感谢提前!

回答by Awesomolocity

Alternatively, you can opt to not load an entire Javascript library for something as simple as this:

或者,您可以选择不为这样简单的事情加载整个 Javascript 库:

document.querySelector('#button').addEventListener('onclick', exit_alert);

回答by Sleep Deprived Bulbasaur

You can do this with jQuery

你可以用 jQuery 做到这一点

$( "#targetId" ).click(function() {
  alert( "Handler for .click() called." );
});