最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - javascript setAttribute style - Stack Overflow

programmeradmin2浏览0评论

I don't get how the two following javascript/css codes can produces different result:

1st:

prev.setAttribute('style', 'position:absolute;left:-70px;opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');

2nd:

            prev.setAttribute('style', 'opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');
            prev.setAttribute('height', size);
            prev.setAttribute('width', size);
            prev.setAttribute('id', 'thumb'+i);
            prev.setAttribute('position', 'absolute');
            prev.setAttribute('left', '-70px');

in the 2nd one, position and left are completely ignored. The result are the same for having the 2 lines of codes and not having them.

It only works if I put prev.style.left, the same thing with position. However setAttribute works for height and width. I really need to know why

I don't get how the two following javascript/css codes can produces different result:

1st:

prev.setAttribute('style', 'position:absolute;left:-70px;opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');

2nd:

            prev.setAttribute('style', 'opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');
            prev.setAttribute('height', size);
            prev.setAttribute('width', size);
            prev.setAttribute('id', 'thumb'+i);
            prev.setAttribute('position', 'absolute');
            prev.setAttribute('left', '-70px');

in the 2nd one, position and left are completely ignored. The result are the same for having the 2 lines of codes and not having them.

It only works if I put prev.style.left, the same thing with position. However setAttribute works for height and width. I really need to know why

Share Improve this question asked Sep 29, 2013 at 16:18 user308553user308553 1,2504 gold badges18 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

position and left are not attributes, they are styles.

width, height and id can be used as attributes or styles, which is why they work in your second example.

发布评论

评论列表(0)

  1. 暂无评论