javascript - jquery创建的子节点用jquery获取不到是什么原因
天蓬老师
天蓬老师 2017-04-11 10:13:53
0
3
326

$(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;
      }

});
});

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

membalas semua(3)
迷茫
  ...
  $(".center-gold-pic").on("click",function(){
    var p=document.getElementById("animation-pic");
  ...
刘奇

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

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

$('<p>')
迷茫

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

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan