.html()
JQuery でメソッドを使用してスペースを挿入しようとすると、問題が発生します。以下は私のコードです:
html += '<td >'+description+". "+location;
html += +" "+position;
html += +" "+side+'</td>';
$('#tempResult').html(html);
私が得ている結果は次のとおりです。
Green Tint. 0FrontNaNRight
.html()
JQuery でメソッドを使用してスペースを挿入しようとすると、問題が発生します。以下は私のコードです:
html += '<td >'+description+". "+location;
html += +" "+position;
html += +" "+side+'</td>';
$('#tempResult').html(html);
私が得ている結果は次のとおりです。
Green Tint. 0FrontNaNRight
+
文字列から演算子を削除します。+=
は文字列の連結を処理するため、追加の+
符号は単純に文字列を正の値にしようとします (これにより、インタープリターは文字列をNaN
数値ではなく - に変更します)。
a += b
は、 の「簡単な方法」(おそらく単純化) ですa = a + b
。
html += '<td >'+description+". "+location;
html += " "+position;
html += " "+side+'</td>';
$('#tempResult').html(html);
+=+ビットは何らかの型変換を行っています。2番目の+を取り除きます。
HTMLを構築する別の方法は、配列と結合を使用することです。一例:
var description = 'DESCRIPTION',
location = 'LOCATION',
position = 'POSITION',
side = 'SIDE',
html = [
'<td>' + description,
'. ' + location,
' ' + position,
' ' + side,
'</td>'
];
$('#tempResult').html(html.join(''));