【问题标题】:How to insert space in html?如何在html中插入空格?
【发布时间】:2012-08-16 21:51:03
【问题描述】:

当我尝试在 JQuery 中使用 .html() 方法插入空格时遇到问题。 以下是我的代码:

html += '<td >'+description+". "+location;
html += +" "+position;
html += +" &nbsp; "+side+'</td>'; 
$('#tempResult').html(html);

我得到的结果如下: Green Tint. 0FrontNaNRight

【问题讨论】:

    标签: javascript jquery-ui jquery jquery-selectors


    【解决方案1】:

    从您的字符串中删除 + 运算符。 += 负责字符串连接,因此附加的 + 符号只是试图使字符串为正数(导致解释器将其更改为 NaN - 不是数字)。

    a += ba = a + b 的“速记方式”(也许是简化)。

    html += '<td >'+description+". "+location;
    html += " "+position;
    html += " &nbsp; "+side+'</td>'; 
    $('#tempResult').html(html);
    

    【讨论】:

    • 我的错,我尝试了很多,但没有注意到额外的加号。非常感谢@nbrooks,它有效:)
    【解决方案2】:

    += + 位正在做一些类型转换。去掉第二个+。

    另一种构建 html 的方法是通过数组和连接。一个例子:

    var description = 'DESCRIPTION',
        location = 'LOCATION',
        position = 'POSITION',
        side = 'SIDE',
        html = [
            '<td>' + description,
            '. ' + location,
            ' ' + position,
            ' ' + side,
            '</td>'
        ];
    
    $('#tempResult').html(html.join(''));
    

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2012-04-05
      • 2011-11-21
      • 2021-05-23
      • 2015-02-08
      相关资源
      最近更新 更多