$(document).ready(function() {
//var p = document.querySelector('#animation-pic');
$(".center-gold-pic").on("click",function(){
var p=$("#animation-pic");
if (p.hasClass('stop')){
p.removeClass('stop');
timer=setInterval(function(){
var left = Math.ceil(Math.random() * (9 - 0) + 0) * 22;
var top = Math.ceil(Math.random() * (9 - 0) + 0) *22;
$.post('ajax.txt', function(data) {
if(data==1){
var imgObj=$(document.createElement("p")) ;
imgObj.css({
"left":left+40,
"top":top+30,
"background-repeat":"no-repeat",
"background-position":"center",
"background-size":"cover",
"height":"35px",
"width":"35px",
"border-radius":"100%",
"position":"absolute",
"background-image":"url(./imgs/images/7.png)",
});
$("#scan-gold-picc").append(imgObj);
}else{
setTimeout(function(){show1();}, 4000)
};
});
},1000);
var lengths=$("#scan-gold-picc").children().length;
console.log(lengths)//这里打印节点数目
if(lengths>6){
}
}else{
p.addClass('stop');
clearInterval(timer);
return;
}
});
});
setInterval延迟1s执行,你获取children长度那一句在插入前就执行了
补充一下,你其实可以这样用jQuery创建dom的
这有一个window.ready和window.onload的区别