我有一個包含標籤的div(如stackoverflow上的標籤功能)。我使用ajax將新標籤附加到這個外部div。一切工作正常,除了有一個額外的左邊距或約5px的添加到新添加的標籤。當我檢查html時,沒有保證金或填充列表,但它肯定存在。 Chrome,Firefox,Safari和Opera也會發生同樣的情況。這裏是我的代碼:爲什麼jQuery append()爲「ajaxed」數據添加左邊距/填充?
HTML:
<div class="tags_wrap">
<div class="tag">
<div class="tag_wrap">Tag 3</div>
</div>
<div class="tag">
<div class="tag_wrap">Tag 2</div>
</div>
...
</div>
CSS:
.tags_wrap {
margin:1px 7px 5px 0px;
float:left;
text-align:left !important;
}
.tag {
padding:0;
margin:4px 7px 0 0;
border: 1px solid #094C7C;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
background-color:#fff;
color:#000;
position:relative;
display:inline-block;
font-size:12px;
}
.tag_wrap {
position:relative;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border-top: 1px solid #1C7ABF;
border-right: 1px solid #1C7ABF;
border-bottom: 1px solid #1C7ABF;
border-left: 1px solid #1C7ABF;
}
的jQuery:
$(document).ready(function(){
$('form#addSomeTags').unbind('submit').bind('submit',function() {
var newTag = $("form#addSomeTags > input[name=tagField]").val();
$.ajax({
type: "POST",
url: 'add_tag.php',
dataType: 'html',
data: {tag:newTag},
success: function(data){
$('.tags_wrap').append(data);
}
}
});
return false;
});
});
的data
我的PHP腳本看起來成功完成後返回.ajax()
像其他標籤一樣:
<div class="tag">
<div class="tag_wrap">Tag 2</div>
</div>
在新的「ajaxed」標籤上始終存在這種不需要的左邊距或填充。如果我刷新頁面,則此邊距不再存在。任何想法爲什麼發生這種情況?
Ajax數據不處理風格。檢查輸出並查找nbsps或類似 – Marko 2013-05-06 02:05:14
我試圖使用$ .trim(),但空間仍然存在。我還在服務器端使用了PHP trim(),然後將其回送給客戶端,但那也不起作用。 – 2013-05-06 02:45:54