从变量中设置style.maxWidth
Setting the style.maxWidth from a variable
我的代码是:
<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>
这会提醒一个宽度为img.png的窗口。问题是:如何为我需要的元素设置这个宽度值。
类似于:
document.getElementById('123').style.maxWidth = this.width;
代替:
alert(this.width);
图像的width
DOM属性将是一个数字。
CSS max-width
属性只接受长度作为其值。
你需要在你的数字中添加单位。
= this.width + "px";
相关文章:
- document.getElementById.style.backgroundImage not working
- 为什么我可以'不要使用xx[this.index].style.display?它's说“;无法读取属性
- HEAD中LINK和STYLE元素的求值顺序
- 如何让firefox使用style['attribute name']=设置样式属性
- 更改SVG'style'元素
- "无法获取属性'的值;style'"当试图在IE中更改样式时
- placeHolder id未使用style.display显示
- 重置transform:rotate()而不调用-webkit transition:style
- 如何使主体采用等同于 style=“background-color: returnBlue()” 的样式属性,其中 r
- Vue.js-插值错误-如何将样式修改为v-bind:style
- Javascript style.background在IE中不起作用
- 为什么当我分配给element.style时,我的元素的样式没有改变
- Greasemonkey:当style=“display: none;”发生变化时如何做某事
- 什么's D3.js中selection.style和selection.attr之间的区别
- javascript无法读取属性'style'为null
- Jhon Papa Angular Style guide Factory-对象内部的函数未运行
- 滚动字幕未捕获类型错误:无法读取属性'style'的未定义
- 基于Blob的'链接样式表'与标准'style'标签
- JavaScript中的style.clip会随着时间的推移而变化
- 从变量中设置style.maxWidth