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 03 Answers
Reset to default 3You 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
版权声明:本文标题:javascript - JQuery: append div with class that is a var - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745289287a2651693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论