-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

+=+ビットは何らかの型変換を行っています。2番目の+を取り除きます。

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 に答える