简体   繁体   English

Javascript在每个输出的数组值的末尾附加换行符

[英]Javascript appending a line break at the end of every outputted array value

When I run the following code the br is appended to the end of the the dates list. 当我运行以下代码时,br将附加到日期列表的末尾。 I am trying to get the br to append after every date value in the array. 我试图让br在数组中的每个日期值之后追加。 I tried using a for loop but that doesn't seem to work. 我尝试使用for循环,但似乎不起作用。 What am I doing wrong? 我究竟做错了什么?

dates = ['2012-09-06 22:39:29', '2012-09-06 22:41:02', '2012-11-05 15:01:10', '2012-11-06 15:37:58', '2012-09-08 10:22:00'];

$('#hi').append(dates.sort() + '<br>');

For loop: 对于循环:

for(i = 0; i < dates.length; i++) {
    $('#hi').append(dates.sort() + '<br>');
}

Try: 尝试:

dates.sort();
$("#hi").append(dates.join("<br />") + "<br />");

I don't know what you think dates.sort() does, but you're using it weird. 我不知道你认为dates.sort()什么,但是你使用它很奇怪。

That's because your'e effectively concatenating the whole array with the BR tag. 那是因为你有效地将整个阵列与BR标签连接在一起。 consider the following exapmle: 考虑以下问题:

  dates.sort();
    for(i = 0; i < dates.length; i++) {
        $('#mydiv').append(dates[i] + '<br>');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM