最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert space in html? - Stack Overflow

programmeradmin7浏览0评论

I am facing issue when I try to insert space using .html() method in JQuery. Following is my code :

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

The result I am getting is as follows: Green Tint. 0FrontNaNRight

I am facing issue when I try to insert space using .html() method in JQuery. Following is my code :

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

The result I am getting is as follows: Green Tint. 0FrontNaNRight

Share Improve this question edited Aug 16, 2012 at 21:53 Zoltan Toth 47.7k12 gold badges131 silver badges137 bronze badges asked Aug 16, 2012 at 21:51 LeejoyLeejoy 1,4465 gold badges25 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Remove the + operator from your string. += takes care of the string concatenation, so the additional + sign simply tries to make the string positive (causing the interpreter to change it to NaN - not a number).

a += b is a "shorthand way" (perhaps a simplification) of saying a = a + b.

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

The += + bit is doing some type conversion. Get rid of the 2nd +.

Another way of building html is via arrays and joining. One example:

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

$('#tempResult').html(html.join(''));
发布评论

评论列表(0)

  1. 暂无评论