简体   繁体   中英

jQuery array displaying separation commas

I have the following jQuery array, with the HTML code for multiple images stored in it:

var images = [ "my first image's code, second image's code, etc, etc" ];

I then have them displayed like so:

$('.gallary').prepend(''+images+'');

This works perfectly, except for one thing; the commas (,) which separate the image codes within the array show up on the site between the displayed images. What am I doing wrong here?

If comma is your problem, it is so because when an Array is typecasted to String, Array.join gets called, which joins with default separator, which is a comma ,

So you've to manually add a empty separator, if you don't want them and you can safely remove those extra string quotes.

$('.gallary').prepend(images.join('')); // should remove the ,

Here are other ways that should work wore efficiently:

ADORABLE DEMO

var images = ["<img src='http://placekitten.com/g/200/300'>",
              "<img src='http://placekitten.com/g/200/300'>",
              "<img src='http://placekitten.com/g/200/300'>",
              "<img src='http://placekitten.com/g/200/300'>"].join('');
// with jquery
$(images).appendTo('.gallery');

plain js is fast too:

// equiv to `$(images).appendTo(thing);`

document.querySelector('.gallery').innerHTML += images;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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