开发者

jquery how to append multiple var's to a div?

开发者 https://www.devze.com 2023-02-26 20:08 出处:网络
i have some var\'s: var1 = (\'1\'开发者_如何学编程); var2 = (\'2\'); var3 = (\'3\'); how to append them to a div using jquery appendTo ?

i have some var's:

var1 = ('1'开发者_如何学编程);
var2 = ('2');
var3 = ('3');

how to append them to a div using jquery appendTo ?

var1, var2, var3.appendTo('.div');

thanks

edit:

var1 = $('<div class="1"></div>');
var2 = $('<div class="2"></div>');
var3 = $('<div class="3"></div>');

i want it to look like this:

<div class="div">
<div class="1"></div>
<div class="2"></div>
<div class="3"></div>
</div>

i can use:

var1.appendTo('.div');
var2.appendTo('.div');
var3.appendTo('.div');

but what if i have 15 vars?? and append alone doesn't do the job


$('.div').append(var1, var2, var3)

whatever a var is


Maybe you can use the append function instead:

    $('.div').append(var1).append(var2).append(var3);


$('.div').append(var1).append(var2).append(var3)


According to docs:

.append( content [, content ] )

content

Type: htmlString or Element or Text or Array or jQuery

Passing array is more convenient.

var items = [];

for(let i = 0; i < count; i++){
   items.push($('<div />', { class: i}));
}

$('.div').append(items);
0

精彩评论

暂无评论...
验证码 换一张
取 消