简体   繁体   English

jQuery:将div附加为var类

[英]JQuery: append div with class that is a var

I have a line that appends a new div to an existing one. 我有一行,将一个新的div追加到现有的。 However I also want to add a class to the new div. 但是我也想向新的div添加一个类。 The class's name is actually held in a var. 该类的名称实际上保存在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中的句点

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

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