简体   繁体   中英

JQuery: append div with class that is a var

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!

您正在串联一个字符串,它的完成方式如下:

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

JS使用+连接项目,如下所示:

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

串联是javascript中的+号,而不是PHP中的句点

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