简体   繁体   English

jQuery-追加HTML不会在div内追加空字符串

[英]jquery - append html doesn't append the empty String inside the div

I am doing a chat project in java . 我正在用java做一个chat project

Using my Server response through jquery-ajax , I am creating an html and append the html to div . 使用通过jquery-ajax Server response ,我正在创建一个html append该html appenddiv

My jquery - html alert without empty string -name(note the id='user' in the alert) will be , 我的jquery - html警报without empty string -name(请注意警报中的id='user' )将为,

在此处输入图片说明

after appending this in the chat area , after appendingchat area

在此处输入图片说明

After jquery-html alert with empty string -name(note the id='user' in the alert) will be , with empty string -name(请注意警报中的id='user' )的jquery-html警报后,

在此处输入图片说明

after appending this in the chat area , after appendingchat area

在此处输入图片说明

My jquery for creating the HTML will be , 我创建HTML的jquery是,

function send(name , message , time){
    //name = $("#name").val();
    //message = $("#message").val();

    var user = "<div id='user' class='fontStyle'>"+name+"</div>";
    var msg = "<div id='msg'  class='fontStyle'>"+message+"</div>";
    var timeStamp = "<div id='time'  class='fontStyle'>"+time+"</div>";

    var row = "<div id='msgSet' >"+ user + msg + timeStamp +"</div>";

    alert(row);
    return row;

}

There is no empty string will be present in chat area . chat area中将没有空字符串。

Hope our stack members will help me. 希望我们的堆栈成员能帮助我。

You can escape html your string so it keeps the white spaces. 您可以转义html字符串,以保留空格。 See details here . 在这里查看详细信息。

Or you can trim your message string (=remove spaces at the beginning and end of it) with such an instruction, and then manualy add the spaces (with the &nbsp; code inside a div, span, ...): 或者,您可以使用这样的指令来修剪消息字符串(=删除其开头和结尾处的空格),然后手动添加空格(在div,span等中间添加&nbsp;代码):

s.replace(/\s+/g, ' ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM