Javascript字符串连接

Javascript字符串连接,javascript,twitter-bootstrap,popover,Javascript,Twitter Bootstrap,Popover,我对字符串连接有问题。我正在进行一系列ajax调用,并根据结果构建一个表,其中每个元素都有一个bootstrap popover字段。在这个领域,我想展示更多的细节。代码如下: ...initiate ajax post ... ... other parameters... function(data){//function called on success var popoverContent = 'Sent: '; popoverContent = pop

我对字符串连接有问题。我正在进行一系列ajax调用,并根据结果构建一个表,其中每个元素都有一个bootstrap popover字段。在这个领域,我想展示更多的细节。代码如下:

...initiate ajax post ...
... other parameters...
function(data){//function called on success
        var popoverContent = 'Sent: ';
        popoverContent = popoverContent.concat(JSON.stringify(obj.value));
        popoverContent = popoverContent.concat('\nReceived: ');
        popoverContent = popoverContent.concat(JSON.stringify(data.error));
        console.log(popoverContent);

... other processing ...
...building table...

'<td> <a class="btn large primary" rel="popover" data-content='+popoverContent+' data-original-title="Detailed description">'+outcome+'</a></td>'+ ...

...rest of the code ...
…启动ajax post。。。
... 其他参数。。。
函数(数据){//成功调用函数
var popcovercontent='Sent:';
popoverContent=popoverContent.concat(JSON.stringify(obj.value));
popoverContent=popoverContent.concat('\n收到:');
popoverContent=popoverContent.concat(JSON.stringify(data.error));
控制台日志(popoverContent);
…其他处理。。。
…建立表格。。。
''+结果+''+。。。
…代码的其余部分。。。
现在我的问题是,在控制台中,popoverContent包含了我想要以字符串形式显示的所有数据,而在popover only Sent:gets display中,如果我将popoverContent设置为与任何其他连接部分相等,它将显示该部分,但整个内容不会显示。
这里缺少什么?

您可以使用+=运算符,而不是在
控制台.log调用之前使用的运算符,它通常更易于阅读(虽然不是必需的),如下所示:

var popoverContent = 'Sent: ';
popoverContent += JSON.stringify(obj.value);
popoverContent += '\nReceived: ';
popoverContent += JSON.stringify(data.error);
然而,真正的问题在于您实际的HTML输出。您没有用“s”来包围结果。事实上,它也需要转义。例如:

... '<td>' +
  '<a class="btn large primary" rel="popover" data-content="' +
      popoverContent.replace(/&/g, '&amp;').replace(/"/g, '&quot;')
     .replace(/\n/g, '<br/>') +
    '" data-original-title="Detailed description">' +
    outcome +
  '</a>' +
'</td> + ...
…”+

“.concat()是一个用于连接两个数组而不是字符串的函数。@DeckerBrower-不正确,还有一个method@MichaelGeary-哦,对不起,我的不好。我只是从来没有见过有人使用过它,原因很明显。谢谢你指出这一点。不用担心,它实际上也让我大吃一惊!MDN绝对不推荐它。你尝试过更新内容吗根据我的建议?如果PopCover内容周围没有“s”,则只会显示第一个单词(在本例中为“Sent:”)你的更新起到了关键作用:D.我如何插入新行?我不确定。我很了解JS和HTML,而不是Twitter引导…它们可能有自己的语法。如果没有现成的文档,我会尝试\r\n、\n或
。你的答案的第一部分是错误的。
.concat
调用只起一个作用考虑到这个代码:<代码> var s=“a”;s= S.CutAT('B');控制台.log(s);打印<代码> AB '/COD>。也许您提到的引用问题是实际问题吗?酷,刚刚看到您的更新。事实上,MDN提到 += 可能比
,这是使用它的另一个很好的理由。只是帮助澄清建议和错误修复。:-)