我有以下代码:
var $element1 = $('#selector'),
$element2...;
var $elements = [ $element1, $element2, $element3 ],
$classes = ['class1','class2','class3'],
$newElements = [];
$.each($elements, function($i, $element){
$newElements.push('<li class="'+ $classes[$i]+'">' + $element +'</li>');
});
$(body).append( $newElements );
输出为:
<li class="class1">[object Object]</li>
<li class="class2">[object Object]</li>
<li class="class3">[object Object]</li>
如何打印实际元素?
答案 0 :(得分:1)
使用附加元素的outerHTML
:
$.each($elements, function($i, $element){
$newElements.push('<li class="'+ $classes[$i]+'">' + $element[0].outerHTML +'</li>');
});