admin管理员组文章数量:1434380
My cod is this:
<img src="img.png">
<dd id="123">COMMENT</dd>
<script>
var img = new Image();
img.onload = function () {
alert(this.width);
}
img.src = 'img.png';
</script>
This alerts a window with width of an img.png. The question is: how to set this width value for an element I need.
Something like:
document.getElementById('123').style.maxWidth = this.width;
Instead of:
alert(this.width);
My cod is this:
<img src="img.png">
<dd id="123">COMMENT</dd>
<script>
var img = new Image();
img.onload = function () {
alert(this.width);
}
img.src = 'img.png';
</script>
This alerts a window with width of an img.png. The question is: how to set this width value for an element I need.
Something like:
document.getElementById('123').style.maxWidth = this.width;
Instead of:
alert(this.width);
Share
Improve this question
edited Jan 7, 2016 at 15:34
Will Vousden
33.4k9 gold badges88 silver badges97 bronze badges
asked Jan 7, 2016 at 15:32
ХомичусХомичус
332 silver badges8 bronze badges
1
-
2
document.getElementById('123').style.maxWidth = this.width;
So what happened when you tried that? – Frédéric Hamidi Commented Jan 7, 2016 at 15:33
1 Answer
Reset to default 3The width
DOM property of an image is going to be a number.
The CSS max-width
property only accepts a length as its value.
You need to add units to your number.
= this.width + "px";
本文标签: javascriptSetting the stylemaxWidth from a variableStack Overflow
版权声明:本文标题:javascript - Setting the style.maxWidth from a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745621745a2666724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论