如何使用ajax动态创建Div id

时间:2015-11-17 08:13:09

标签: jquery ajax

这是我的代码:

id  select_type table   type    possible_keys   key key_len ref rows     Extra
1   SIMPLE  n   ref status,cid  status  1   const   13  Using index condition; Using temporary; Using file...
1   SIMPLE  c1  eq_ref  PRIMARY,status_special  PRIMARY 4   tb.n.cid              1 Using where
1   SIMPLE  nn  eq_ref  PRIMARY PRIMARY 4   tb.n.parent 1   NULL
1   SIMPLE  c2  eq_ref  PRIMARY PRIMARY 4   tb.nn.cid   1   NULL
1   SIMPLE  ca  ref cid_aid cid_aid 4   tb.n.cid    53  Using where;   Using index
1   SIMPLE  a   eq_ref  PRIMARY,status_show_date    PRIMARY 4   tb.ca.aid   1   Using where

点击按钮后如何创建function getcategory(id){ $.ajax({ url: "ajax.php", method: 'post', data: { id: id, cat: 'category' }, success: function(result){ $("#category").html(result); } }); } #category#category1#category2

1 个答案:

答案 0 :(得分:0)

在将结果附加到html()

之前,您可以在成功方法中附加带有id(counter)的div元素

这将在类别div中添加一个新的子div,并在该

中添加打印结果



var counter=0;
function addDiv()
{
	counter++;
    $("#cat").append("<div id='counter"+counter+"'></div>");
    $("#counter"+counter).html("<h>Hello All</h>");
    
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="cat">
</div>
<input type="button" onclick="addDiv()" value="add">
&#13;
&#13;
&#13;