这样创建节点添加属性正确吗??

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.attr({

                  "left":left+40,

                  "top":top+20,

                  "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-pic").append(imgObj);

    

    我要的需求就是发送一次请求创建一个div节点;节点的内容就是一张图片;上面是我通过.attr()方法给节点添加属性;发现无法宣示;求大神指教;    

            

            

            

            


临摹微笑
浏览 573回答 1
1回答

翻过高山走不出你

imgObj.attr({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "left":left+40,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "top":top+20,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "background-repeat":"no-repeat",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"background-position":"center",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"background-size":"cover",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"height":"35px",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "width":"35px",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "border-radius":"100%",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position":"absolute",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"background-image":"url(./imgs/images/7.png)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });应该是这一块错了,应该用css。attr是设置属性节点的。设置完是这样<div&nbsp;left:''&nbsp;right=''></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript