简体   繁体   English

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

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

Suppose I have an alert message like: 假设我有一条警告消息,例如:

  alert("Message"); 

When this command is executed, an alert box pops up but the message is aligned to the left of the alert box. 执行此命令时,将弹出一个警报框,但消息与警报框的左侧对齐。

Is there a way to center this message inside the alert box? 有没有办法将此消息放在警报框中?

If your alert message will be static then add multiple '\\t' at the starting of your message. 如果您的警报消息是静态的,则在消息的开头添加多个“ \\ t”。

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

Here \\n is use for break the message and place on next line \\ n用于中断消息并放在下一行

Well, one thing you should know is you can't style the alert box. 好吧,您应该知道的一件事是您无法设置警报框的样式。 it's the system object not a css thing . 这是系统对象而不是CSS东西


if you still want to use alert and want to move your text from left to right use \\t , which is a tab. 如果仍然要使用alert并想从左向右移动文本,请使用\\t ,它是一个标签。 You can figure out how many characters you're going to use on a line and then encase the text in \\t s. 您可以找出要在一行中使用多少个字符,然后将文本用\\t括起来。

eg: 例如:

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

It's not perfect, but it's as close to what one can move text to center in alertBox. 它并不完美,但与将文本移动到alertBox中心的位置一样接近。

\\t works perfect with Firefox but not with Chrome. \\t与Firefox完美搭配,但与Chrome搭配却不完美。 I love Chrome, but as a web developer this is a pain in the neck. 我喜欢Chrome,但是作为一名Web开发人员,这真是令人头疼。


FYI: You can also create your own. 仅供参考:您也可以创建自己的。 For example, 例如,

jQuery UI Dialog jQuery UI对话框

Have a look here : DEMO 在这里看看: 演示

The best thing I used these days to display message is using toast messages. 这些天来我用来显示消息的最好的方法是使用吐司消息。 They pops up, show your message in beautiful box and then pops out in sleek manner. 他们弹出,在漂亮的框中显示您的消息,然后以流畅的方式弹出。

have a look at MATERIALIZE CSS TOASTS 看看MATERIALIZE CSS TOASTS

You should be able to edit the CSS file of the Javascript and centralize the content using Text-alight. 您应该能够编辑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