最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Concat two html elements - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Feb 28, 2014 at 13:49 user1636522 asked Feb 28, 2014 at 13:36 John SmithJohn Smith 6,26919 gold badges61 silver badges113 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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);

In short, in general:

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

Take a look on this DEMO.

发布评论

评论列表(0)

  1. 暂无评论