jquery创建的子节点用jquery获取不到是什么原因

$(document).ready(function() {

//var div = document.querySelector('#animation-pic');

$(".center-gold-pic").on("click",function(){

   var div=$("#animation-pic");
  
if (div.hasClass('stop')){
               div.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("div")) ;
              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{
      div.addClass('stop');
      clearInterval(timer); 
       return;
      }

});
});

阅读 2.3k
3 个回答
  ...
  $(".center-gold-pic").on("click",function(){
    var div=document.getElementById("animation-pic");
  ...

setInterval延迟1s执行,你获取children长度那一句在插入前就执行了

补充一下,你其实可以这样用jQuery创建dom的

$('<div>')
新手上路,请多包涵

这有一个window.ready和window.onload的区别

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题