如何在jquery中将2个动态创建的HTML元素(var foo + bar)连接成html属性?
预期结果:<div class="form-control" data-trigger="fileinput"><i class="glyphicon glyphicon-file fileinput-exists"></i> <span class="fileinput-filename"></span></div>
但我得到了:<div class="form-control" data-trigger="fileinput">[object Object][object Object]</div>
var foo = $('<i></i>', {
'class': 'glyphicon glyphicon-file fileinput-exists'
});
var bar = $('<span></span>', {
'class': 'fileinput-filename'
});
var formControl = $('<div></div>', {
'class': 'form-control',
'data-trigger': 'fileinput',
'html': foo + bar
});
答案 0 :(得分:0)
您需要获取foo
和bar
var foo = $('<i>123</i>', {
'class': 'glyphicon glyphicon-file fileinput-exists'
});
var bar = $('<span>456</span>', {
'class': 'fileinput-filename'
});
var formControl = $('<div></div>', {
'class': 'form-control',
'data-trigger': 'fileinput',
'html': foo[0].outerHTML + bar[0].outerHTML
});
outerHTML
browser compatability
工作Fiddle