-1

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

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

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

4

2 回答 2

9

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

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

html += '<td >'+description+". "+location;
html += " "+position;
html += " &nbsp; "+side+'</td>'; 
$('#tempResult').html(html);
于 2012-08-16T21:53:12.700 回答
1

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

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

var description = 'DESCRIPTION',
    location = 'LOCATION',
    position = 'POSITION',
    side = 'SIDE',
    html = [
        '<td>' + description,
        '. ' + location,
        ' ' + position,
        ' ' + side,
        '</td>'
    ];

$('#tempResult').html(html.join(''));
于 2012-08-16T21:59:24.533 回答