HTMLImageElement: Image() 建構函式
Image() 建構函式建立一個新的 HTMLImageElement 例項。它在功能上等同於 document.createElement('img')。
注意: 這個函式不應與 CSS 中的 image() 函式混淆。
語法
js
new Image()
new Image(width)
new Image(width, height)
引數
用法說明
無論在建構函式中指定了什麼尺寸,整個點陣圖都會被載入。建構函式中指定的尺寸會透過所得例項的 HTMLImageElement.width 和 HTMLImageElement.height 屬性體現。影像的 CSS 畫素的固有寬度和高度透過 HTMLImageElement.naturalWidth 和 HTMLImageElement.naturalHeight 屬性體現。如果在建構函式中沒有指定尺寸,那麼這兩對屬性將具有相同的值。
示例
js
const myImage = new Image(100, 200);
myImage.src = "picture.jpg";
document.body.appendChild(myImage);
這相當於在 <body> 中定義了以下 HTML 標籤:
html
<img width="100" height="200" src="picture.jpg" />
規範
| 規範 |
|---|
| HTML # dom-image-dev |
瀏覽器相容性
載入中…