簡體   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