jQuery追加预先格式化的连接字符串

时间:2013-08-14 19:15:11

标签: javascript jquery html

for (i = 0; i < json.people[NPC_id].dialogs.start.options.length; i++) {
    concat += "\n "+ optionNumbers + ") " + json.people[NPC_id].dialogs.start.options[i].text;
    optionNumbers++;    
}
//append JSON dialog to label...
options = concat;

我在这里更新HUD:

function updateHUD() {      
    console.log(options);
    $('#options').html(options);        
}

然后更新HTML:

<div id="bottomDisplay">
    <ul>
        <li><b>Options:</b> <div id="options"></div>    </li>
    </ul>
</div>

Console.log提供格式化的concat字符串...

1) What is wrong with it?
2) What is a 狗?
3) Sorry, I don't have time :/

但是html出现在未格式化的div id中:

1) What is wrong with it? 2) What is a 狗? 3) Sorry, I don't have time :/

为什么?

1 个答案:

答案 0 :(得分:4)

HTML不处理多个空格或新行。

你可以:

  • 将整体放在<pre>元素中(最简单的就是拥有<pre id=options></pre>
  • \n替换为<BR>options = options.replace(/\n/g,'<br>')