繁体   English   中英

如何在JavaScript中将消息居中放置在警报框中?

[英]How to center the message inside an alert box in JavaScript?

假设我有一条警告消息,例如:

  alert("Message"); 

执行此命令时,将弹出一个警报框,但消息与警报框的左侧对齐。

有没有办法将此消息放在警报框中?

如果您的警报消息是静态的,则在消息的开头添加多个“ \\ t”。

alert('\t Quantity should be less than Remaining Quantity! \n \t\t\t Remaining Quantity');

\\ n用于中断消息并放在下一行

好吧,您应该知道的一件事是您无法设置警报框的样式。 这是系统对象而不是CSS东西


如果仍然要使用alert并想从左向右移动文本,请使用\\t ,它是一个标签。 您可以找出要在一行中使用多少个字符,然后将文本用\\t括起来。

例如:

\t This text will be centered \t\n
\t in the middle of the alert \t

它并不完美,但与将文本移动到alertBox中心的位置一样接近。

\\t与Firefox完美搭配,但与Chrome搭配却不完美。 我喜欢Chrome,但是作为一名Web开发人员,这真是令人头疼。


仅供参考:您也可以创建自己的。 例如,

jQuery UI对话框

在这里看看: 演示

这些天来我用来显示消息的最好的方法是使用吐司消息。 他们弹出,在漂亮的框中显示您的消息,然后以流畅的方式弹出。

看看MATERIALIZE CSS TOASTS

您应该能够编辑Javascript的CSS文件并使用Text-alight集中内容。

#popup_container {
  font-family: Arial, sans-serif;
  font-size: 15px;
  min-width: 300px; /* Dialog will be no smaller than this */
  max-width: 600px; /* Dialog will wrap after this width */
  background: #FFF;
  border: solid 5px #999;
  **text-align:center !important;**
  color: #000;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px; 
}

暂无
暂无

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

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