Using JavaScript to create a Chrome-style alert

帅比萌擦擦* 提交于 2019-12-11 12:40:06

问题


How do I create an alert in Chrome using JavaScript that opens a text box in the middle of the browser window and washes out the background, like so:

rather than an alert that just pops up an entirely new window (in my case in the Linux Mint GTK theme):

If I type alert() into the console, I get an alert that looks like the latter, in a separate window.


回答1:


You should use the dialog element. Something like this:

HTML:

<dialog>
  This is a dialog.<br>
  <button>Close</button>
</dialog>

Javascript:

var dialog = document.querySelector("dialog")
dialog.querySelector("button").addEventListener("click", function() {
  dialog.close()
})
dialog.showModal()


来源:https://stackoverflow.com/questions/32079003/using-javascript-to-create-a-chrome-style-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!