Javascript 从变量设置style.maxWidth
我的cod是:Javascript 从变量设置style.maxWidth,javascript,css,variables,Javascript,Css,Variables,我的cod是: <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> 而不是: alert(this.width); 图像的widthDOM属性将是一个数字 CSSmax
<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>
而不是:
alert(this.width);
图像的
width
DOM属性将是一个数字
CSSmax width
属性只接受长度作为其值
您需要将单位添加到您的号码中
= this.width + "px";
document.getElementById('123').style.maxWidth=this.width代码>那么当你试着那样做时发生了什么?谢谢!你让我开心。
= this.width + "px";