jQuery UI对话框文本是否有换行选项?

Eth*_*vis 11 html javascript jquery

我正在使用来自Pokemon的数据创建一个网站并尝试执行一个对话框.我尝试在文本中使用JS换行符:

function alertBox(monster) {
    $("#dialog").dialog();
    $("#dialog").dialog("option", "title", monster);
    $("#dialog").text("Height: "  + pokemon.height[monster] + "\n" +
                      "Weight: " + pokemon.weight[monster]);
}
Run Code Online (Sandbox Code Playgroud)

...而且我也尝试过使用html换行标记:

function alertBox(monster) {
    $("#dialog").dialog();
    $("#dialog").dialog("option", "title", monster);
    $("#dialog").text("Height: "  + pokemon.height[monster] + "<\br>" +
                      "Weight: " + pokemon.weight[monster]);
}
Run Code Online (Sandbox Code Playgroud)

但似乎都没有返回我正在寻找的换行效果!JS换行符只是一个空格,而html换行标记只是连接到字符串.有没有办法在对话框文本中强制换行?

Max*_*ter 8

jQuery .text()函数自动转义HTML实体,因此您的<br />标记不再被解释为HTML,而是转换为转义文本.

要防止此HTML转义,您需要使用.html()而不是.text()设置内容:

function alertBox(monster) {
    $("#dialog").dialog();
    $("#dialog").dialog("option", "title", monster);
    $("#dialog").html("Height: "  + pokemon.height[monster] + "<br />" +
                      "Weight: " + pokemon.weight[monster]);
}
Run Code Online (Sandbox Code Playgroud)