如何在多行HTML中制作警报框

时间:2019-04-05 22:29:24

标签: javascript html newline alert

有人知道如何在具有多行的HTML中制作一个警报框吗?我正在寻找这样的输出: 规则:
1. Blah Blah Blah
2. Blah Blah Blah
3. Blah Blah Blah
[取消] [确定]

这是到目前为止的代码:

function rules() {
  agree = confirm("RULES: 1. Blah Blah Blah 2. Blah Blah Blah 3. Blah Blah Blah Press OK to agree and Cancel to Dissagree")
  if (agree == true) {
    alert("Thank you for agreeing to the rules!")
  } else {
    alert("You can not move on unless you agree to the rules.")
  }
}
<button onclick="rules()">Agree to the Rules</button>

但是它与一行上的所有内容混淆。有谁知道如何制作它,以便在其中使用换行符,例如<br>

4 个答案:

答案 0 :(得分:2)

alert(“这是第一行\ n这是第二行\ n这是第三行”);

答案 1 :(得分:1)

插入\ n创建新行

function rules() {
  agree = confirm("RULES: 1. Bl\nah Blah Blah 2. Bla\nh Blah Blah 3. Bla\nh Blah Blah Press OK to agree and Cancel to Dissagree")
  if (agree == true) {
    alert("Thank you for agreeing to the rules!")
  } else {
    alert("You can not move on unless you agree to the rules.")
  }
}
<button onclick="rules()">Agree to the Rules</button>

答案 2 :(得分:1)

您将使用'\n'创建行。

例如,您的规则字符串为:

'Rules:\n1. Blah Blah Blah\n2. Blah Blah Blah\n3. Blah Blah Blah'

答案 3 :(得分:0)

  

经过研究,我找到了问题的答案。

要轻松创建包含多行的警报框,请使用\n

function rules() {
  agree = confirm("RULES: \n1. Blah Blah Blah \n2. Blah Blah Blah \n3. Blah Blah Blah \nPress OK to agree and Cancel to Dissagree")
  if (agree == true) {
    alert("Thank you for agreeing to the rules!")
  } else {
    alert("You can not move on unless you agree to the rules.")
  }
}
<button onclick="rules()">Agree to the Rules</button>

我希望这对想知道这个的人有所帮助!