Concat 2将元素创建为html属性

时间:2015-02-16 16:52:17

标签: javascript jquery html concat

如何在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
});

1 个答案:

答案 0 :(得分:0)

您需要获取foobar

的外部HTML
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