Javascript執行個體教程:width和style.width

來源:互聯網
上載者:User

文章簡介:今天在寫一個修改圖片寬度的demo的時候碰到一個問題,我通過擷取image對象後,直接設定屬性image.style.width=100,沒有生效,後來查出原因,因為 style.width 所接收的為字串,image.width=100,這種寫法是可以的,因為 width 本身就是 number 類型的。

 今天在寫一個修改圖片寬度的demo的時候碰到一個問題,我通過擷取image對象後,直接設定屬性image.style.width=100,沒有生效,後來查出原因,因為 style.width 所接收的為字串,image.width=100,這種寫法是可以的,因為 width 本身就是 number 類型的。

然後看到一個關於 style.width 的設值的說法,如下

IE
id.style.width = "100"; //只帶數值,不會出錯
id.style.width = "100px"; //帶數值和單位,不會出錯
id.style.width = "100px;"; //帶數值、單位和分號,會出錯

FF
id.style.width = "100"; //只帶數值,會出錯
id.style.width = "100px"; //帶數值和單位,不會出錯
id.style.width = "100px;"; //帶數值、單位和分號,不會出錯

所以我們平時寫的時候,最好是使用“數值+單位”的這種寫法,以免出現不可預料的錯誤。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.