[英]how to add the html tag using jquery
我已經使用它編寫了以下功能,我在HTML中顯示了ajax響應
function loadData() {
$.ajax({
type: "post",
url: "http://127.0.0.1/get_scroll_rec/",
cache: false,
data:'',
success: function(response){
var obj = JSON.parse(response);
try{
var str = '';
var items=[];
$.each(obj[0], function(i,val){
//Here display code
items.push($('<div id="recent">').html(val.Title));
items.push($('<h1>').html(val.name));
items.push($('<p>').html(val.desciption));
items.push($('<div id="recent_created" class="recent_created">').html(val.added_on));
});
$('#recent_rightwrapper1').fadeOut('slow', function() {
$(this).append(str).fadeIn('slow').fadeIn(3000);
$('#recent_rightwrapper1').css({backgroundColor: ''});
$('#recent_rightwrapper1').append.apply($('#recent_rightwrapper1'), items);
}).css({backgroundColor: ''});
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
});
我如何向該顯示區域代碼添加一些HTMl標簽,例如(更改為添加到第一行“ h1”標簽,更改為添加到第二行“ a”標簽),希望在沒有第5行的最后一行添加div而沒有數據。
1> items.push($('<div id="recent"><h1>').html(val.Title));
2> items.push($('<h1><a href="" target="_blank">').html(val.name));
3> items.push($('<p>').html(val.desciption));
4> items.push($('<div id="recent_created" class="recent_created">').html(val.added_on));
5> items.push($('<div id="blank">')
您可以通過append
將子節點附加到現有元素。
$("#yourRoot").append($('<div id="blank">'));
編輯: append
適用於任何jQuery對象,也適用於您剛剛通過提供HTML文本創建的對象。 因此,如果要將空div
附加到第一行中的元素,則只需執行以下操作:
var $firstLine = $('<div id="recent"><h1>').html(val.Title);
var $lastLine = $('<div id="blank">');
$firstLine.append($lastLine);
items.push($firstLine);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.