admin管理员组

文章数量:1418593

I have a line that appends a new div to an existing one. However I also want to add a class to the new div. The class's name is actually held in a var.

I am just unsure of the syntax of this - could anyone point me in the right direction at all?

 var itemclass = "myclass";

 $('#wrapper').append('<div class="itemclass"></div>');

So far I have tried:

 $('#wrapper').append('<div class=" . itemclass . "></div>');

 $('#wrapper').append('<div class=" & itemclass & "></div>');

To no effect... any help is appreciated!

I have a line that appends a new div to an existing one. However I also want to add a class to the new div. The class's name is actually held in a var.

I am just unsure of the syntax of this - could anyone point me in the right direction at all?

 var itemclass = "myclass";

 $('#wrapper').append('<div class="itemclass"></div>');

So far I have tried:

 $('#wrapper').append('<div class=" . itemclass . "></div>');

 $('#wrapper').append('<div class=" & itemclass & "></div>');

To no effect... any help is appreciated!

Share Improve this question asked Dec 29, 2012 at 4:29 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

You are concatenating a string, it is done like:

$('#wrapper').append('<div class="' + itemclass + '"></div>');

JS uses + to concatenate items, like this:

 $('#wrapper').append('<div class="' + itemclass + '"></div>');

concatenation is the + sign in javascript, not a period like in PHP

本文标签: javascriptJQuery append div with class that is a varStack Overflow