Concat two html elements

半城伤御伤魂 提交于 2021-01-28 18:11:48

问题


I have this code:

var bread =  $('<li/>').html($('<a/>',{
    href: '#',
    text: 'Alle',
    click: function(){ Diagnose.start() } 
}));
bread += $('<li/>').html($('<a/>',{
    href: '#',
    text: data['icd1'].nummer,
    click: function(){ Diagnose.icd(2,data['icd1'].id) } 
}));

$('#side-panel2 .breadcrumb').html(bread.toString());

The problem is that the ouput is not the html i would like to have but instead its:

<ul class="breadcrumb" style="margin-top: 9px;margin-bottom: 0px;font-size:11px">
 [object Object][object Object]
 .....

First my code looked like this:

$('#side-panel2 .breadcrumb').html($('<li/>').html($('<a/>',{
    href: '#',
    text: 'Alle',
    click: function(){ Diagnose.start() } 
})));                                   
$('#side-panel2 .breadcrumb').append($('<li/>').html($('<a/>',{
    href: '#',
    text: data['icd1'].nummer,
    click: function(){ Diagnose.icd(2,data['icd1'].id) } 
})));

This solution worked but i would like to change the html in one step because with the code from above a little delay can be seen! Thanks


回答1:


You can use .add() - not a concatenation operator because those are jQuery objects not strings

var bread =  $('<li/>').html($('<a/>',{href: '#',text: 'Alle',click: function(){Diagnose.start()} }));
bread = bread.add($('<li/>').html($('<a/>',{href: '#',text: data['icd1'].nummer,click: function(){Diagnose.icd(2,data['icd1'].id)} })));

$('#side-panel2 .breadcrumb').empty().append(bread);



回答2:


In short, in general:

$(".container").append( $("<a>A</a>"), $("<a>B</a>") );

Take a look on this DEMO.



来源:https://stackoverflow.com/questions/22096680/concat-two-html-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!