简体   繁体   English

按钮 onlick 显示警告框

[英]Button onlick show alert box

I have a modal and after pressing the password update button, the modal should close and an alert box should appear on my homepage.我有一个模式,在按下密码更新按钮后,模式应该关闭并且我的主页上应该出现一个警告框。 however, the alert box will not appear when the page is first opened.但是,首次打开页面时不会出现警告框。 how can I do it?我该怎么做? Do you have any examples you can share?你有什么例子可以分享吗?

If you are using the alerts provided by the browser, you have to add a EventListener to your button like:如果您正在使用浏览器提供的警报,则必须向您的按钮添加一个EventListener ,例如:

yourCloseButton.addEventListener("click", () => {
  alert("The modal has been closed!");
});

Complementing Apollo79's answer, you can also use an inline event listener, but this is no longer recommended.补充 Apollo79 的答案,您还可以使用内联事件侦听器,但不再推荐这样做。 There are some specific cases for this, but if you really don't need one, use Apollo79's answer instead.有一些具体情况,但如果您真的不需要,请改用 Apollo79 的答案。

<button id="urButton" onclick="alertAndDisappear()">

or或者

yourCloseButton.onclick = function(){}

which is equivalent to the first one.这相当于第一个。

Again, only use these if you really need them同样,只有在你真的需要它们时才使用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM