CSS javascript setAttribute 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19080296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:33:37  来源:igfitidea点击:

javascript setAttribute style

javascriptcsssetattribute

提问by user308553

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

我不明白以下两个 javascript/css 代码如何产生不同的结果:

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.

在第二个中,位置和左完全被忽略。有 2 行代码和没有它们的结果是一样的。

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

它仅在我放置时才有效prev.style.left,与位置相同。但是 setAttribute 适用于高度和宽度。我真的需要知道为什么

回答by Timm

positionand leftare not attributes, they are styles.

position并且left不是属性,它们是样式。

width, heightand idcan be used as attributes or styles, which is why they work in your second example.

width,height并且id可以用作属性或样式,这就是它们在您的第二个示例中工作的原因。