更改 JavaScript Alert() 或 Prompt() 的外观
有没有办法改变 JavaScript 中警报或提示的外观?例如添加图像、更改字体颜色或大小以及任何使其看起来不同的操作。
Is there any way to change the looks of an alert or prompt in JavaScript? Things like adding an image, changing the font color or size, and whatever will make it look different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
扩展 Matthew Abbott 的想法,令我震惊的是,您想要一个使用普通旧 Javascript 而不依赖任何框架的答案。这是一个快速而肮脏的页面,它发出一个带有简单关闭按钮的 div 和中心显示的消息:
alertBox 和alertClose 按钮的粗略 CSS
然后是一些 javascript 来覆盖内置警报功能并提供关闭功能来处理AlertClose 上的点击事件。
现在,按照 Matthew Abbott 的建议,调用
alert
会显示您刚刚创建的 div,然后单击 x 将其隐藏,使其不可见。要实现,您需要在警报功能中进行测试,以确保您不会重新创建 div 一百万次,并且您需要修改 CSS 以使其适合您的配色方案等,但这是如何实现您自己的警报框的大致形式。
对我来说这似乎有点矫枉过正,我同意 Matthew 的观点,即使用 jQuery 或 YUI 等著名框架创建的某种对话框或模式元素从长远来看会更好,而且更容易。
Expanding on Matthew Abbott's idea, it struck me that you want an answer that uses plain old Javascript without recourse to any frameworks. Here's a quick and dirty page that emits a div with a simple close button and the message presented in the centre:
Rough CSS for the alertBox and the alertClose button
Then some javascript to override the built-in alert function and provide a closing function to handle the click event on alertClose.
Calling
alert
, as Matthew Abbott suggested, now displays the div you've just created and clicking the x dismisses it by making it invisible.To implement, you'd need to have a test in the alert function to make sure you're not re-creating the div a million times and you'd need to mess around with the CSS to make it fit your colour scheme etc, but that's the rough shape of how to implement your own alert box.
Seems like overkill to me and I agree with Matthew that doing it with some kind of dialog or modal element created with a famous framework like jQuery or YUI would be better and strangely easier in the long run.
alert
和prompt
函数调用浏览器提供的 API,因此您无法控制它们的外观。不过,您可以做的是重新定义这些函数,并且可以使用 jQuery 模态之类的东西,例如:
The
alert
andprompt
functions call APIs provided by the browser, so you won't have any control over how they look.What you could do though is redefine those functions, and instead maybe use something like jQuery modal, e.g.:
你无法修改它;但你可以使用类似
showModalDialog
的东西,它只需要一个 URI 来加载你想要的 hmtl/css 表单(如果你可以将其保留在单独的页面中)。You can't modify that; but you can use something like
showModalDialog
, it needs just an URI to load the hmtl/css form you want (if you can keep it in a separate page).我自己的快速运行,扩展了本页上的一些代码:
但这里也有一些很好的答案:
A quick run of my own, expanding on some of the code on this page:
But there are also some good answers here: