我正在尝试使用jquery .append()函数将新数据附加到容器div。但是,附加的输出会修剪创建的标签之间的空格。查看附带的屏幕截图:
如何解决此问题?
Javascript和HTML代码如下:
$("a.add-member").click(function(){
var user = '<div class="row user">'+
'<div class="col-md-6">abc@site.com</div>'+
'<div class="col-md-4">'+
'<div class="user-roles">'+
'<span class="label label-user-role">Role 1</span>'+
'<span class="label label-user-role">Role 2</span>'+
'<span class="label label-user-role active">Role 3</span>'+
'</div>'+
'</div>'+
'<div class="col-md-2 text-center">'+
'<a href="#" class="remove-member"><i class="fa fa-times-circle-o"></i></a>'+
'</div>'+
'</div>';
$("#add-team .users").append(user).hide(0).fadeIn(700);
});
<div class="users">
<div class="row user">
<div class="col-md-6">xyz@site.com</div>
<div class="col-md-4">
<div class="user-roles">
<span class="label label-user-role">Role 1</span>
<span class="label label-user-role">Role 2</span>
<span class="label label-user-role active">Role 3</span>
</div>
</div>
<div class="col-md-2 text-center">
<a href="#" class="remove-member"><i class="fa fa-times-circle-o"></i></a>
</div>
</div>
<div class="row user">
<div class="col-md-6">pqr@site.com</div>
<div class="col-md-4">
<div class="user-roles">
<span class="label label-user-role">Role 1</span>
<span class="label label-user-role">Role 2</span>
<span class="label label-user-role active">Role 3</span>
</div>
</div>
<div class="col-md-2 text-center">
<a href="#" class="remove-member"><i class="fa fa-times-circle-o"></i></a>
</div>
</div>
</div>
答案 0 :(得分:1)
在jQuery代码中添加了空格:
$("a.add-member").click(function(){
var user = '<div class="row user">'+
'<div class="col-md-6">ajinkya.bandagale@gmail.com</div>'+
'<div class="col-md-4">'+
'<div class="user-roles">'+
'<span class="label label-user-role">Admin</span> '+
'<span class="label label-user-role">Observer</span> '+
'<span class="label label-user-role active">Normal</span> '+
'</div>'+
'</div>'+
'<div class="col-md-2 text-center">'+
'<a href="#" class="remove-member"><i class="fa fa-times-circle-o"></i></a>'+
'</div>'+
'</div>';
$("#add-team .users").append(user).hide(0).fadeIn(700);
})
答案 1 :(得分:0)
由于您已经为每个跨度以不同的行编写了HTML代码,但是当您通过创建字符串通过jQuery添加div或任何内容时,它将其视为单行。 因此,如果你在每行添加\ n它将起作用。
user = '<div class="row user">' +
'<div class="col-md-6">pqr@site.com</div>' +
'<div class="col-md-4">' +
'<div class="user-roles">' +
'<span class="label label-user-role">Role 1</span>\n' +
'<span class="label label-user-role">Role 2</span>\n ' +
'<span class="label label-user-role active">Role 3</span>\n ' +
'</div>' +
'</div>' +
'<div class="col-md-2 text-center">' +
'<a href="#" class="remove-member"><i class="fa fa-times-circle-o"></i></a>' +
'</div>' +
'</div>';
$(".users").append(user).hide(0).fadeIn(700);