HTMLImageElement
åºçº¿
广æ³å¯ç¨
*
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
* æ¤ç¹æ§çæäºé¨åçæ¯æç¨åº¦å¯è½ææä¸åã
HTMLImageElement æ¥å£æä¾äºç¹å«ç屿§åæ¹æ³ (å¨å¸¸è§ç HTMLElementä¹å¤ï¼å®ä¹è½éè¿ç»§æ¿ä½¿ç¨) æ¥æçºµ <img> å
ç´ çå¸å±åå¾åã
Constructor
Image()-
Image()æé å¨ï¼å¸¦æä¸¤ä¸ªå¯éçåæ°ï¼åå«è¡¨ç¤ºèµæºç宽度åé«åº¦ï¼å建äºä¸ä¸ªå°æªè¢«æå ¥ DOM æ ä¸çHTMLImageElementå®ä¾ãWhen called without parameters,new Image()is equivalent to callingdocument.createElement("img").
屿§
ä»å®çç¶å
ç´ HTMLElement ç»§æ¿ç屿§ã
HTMLImageElement.alt-
ä¸ä¸ª
String表示 HTML 屿§altï¼è¡¨æå¾åçåå¤æè¿°å 容ï¼ä¼å¨å¾åæ æ³å è½½æ¶æ¾ç¤ºã HTMLImageElement.completeåªè¯»-
è¿åä¸ä¸ª
Boolean妿æµè§å¨å·²ç»ä¸è½½å®æ¯ï¼å¹¶ä¸å¾åæ¯åæ¯æçå¾çç±»åãè§£ç çè¿ç¨ä¸æ²¡æåçé误ï¼åè¿åtrueãThat means this value is alsotrueif the image has nosrcvalue indicating an image to load. HTMLImageElement.crossOrigin-
ä¸ä¸ª
String表示è¿ä¸ª img å ç´ ç CORS 设置ãåè CORS settings attributesãThis may benullif CORS is not used. HTMLImageElement.currentSrcåªè¯»-
è¿åä¸ä¸ª
String表示å è½½å½åæ¾ç¤ºçå¾åç URLã è¿å¯è½ä¼æ¹åï¼å 为å¾åæ¯è°æ´ï¼ç±äºä¸æååçæ¡ä»¶ï¼ç±ä»»ä½ media queries çå°æ¹ã HTMLImageElement.decoding-
An optional
Stringrepresenting a hint given to the browser on how it should decode the image. If this value is provided, it must be one of the possible permitted values:syncto decode the image synchronously,asyncto decode it asynchronously, orautoto indicate no preference (which is the default). Read thedecodingpage for details on the implications of this property's values. HTMLImageElement.height-
ä¸ä¸ªæ´æ°ï¼è¡¨ç¤º HTML 屿§
heightï¼è¯´æäºå¾åå¨ CSS åç´ ä¸æ¸²æçé«åº¦ã HTMLImageElement.isMap-
ä¸ä¸ª
Boolean表示 HTML 屿§ismapï¼è¯´æäºå¾åæ¯æä¸ªæå¡å¨ç«¯å¾åæ å°çä¸é¨åãThis is different from a client-side image map, specified using an<img>element and a corresponding<map>which contains<area>elements indicating the clickable areas in the image. The image must be contained within an<a>element; see theismappage for details. HTMLImageElement.naturalHeightåªè¯»-
è¿åä¸ä¸ªæ´æ°ï¼å¦æå¯ç¨çè¯ï¼è¡¨æå¾åå¨ CSS ä¸åºæçé«åº¦ï¼åä½ä¸ºåç´ ï¼å¦åè¿å
0ã妿å¾çæ¯ä»¥å ¶åæ¥ç大尿¸²æï¼åæ¤å¼çäºå¾ççé«åº¦ã HTMLImageElement.naturalWidthåªè¯»-
è¿åä¸ä¸ªæ´æ°ï¼å¦æå¯ç¨çè¯ï¼è¡¨æå¾åå¨ CSS ä¸åºæç宽度ï¼åä½ä¸ºåç´ ï¼å¦åè¿å
0ã妿å¾çæ¯ä»¥å ¶åæ¥ç大尿¸²æï¼åæ¤å¼çäºå¾çç宽度ã HTMLImageElement.referrerPolicy-
A
Stringthat reflects thereferrerpolicyHTML attribute, which tells the user agent how to decide which referrer to use in order to fetch the image. Read this article for details on the possible values of this string. HTMLImageElement.sizes-
A
Stringreflecting thesizesHTML attribute. This string specifies a list of comma-separated conditional sizes for the image; that is, for a given viewport size, a particular image size is to be used. Read the documentation on thesizespage for details on the format of this string. HTMLImageElement.src-
ä¸ä¸ª
String表示 HTML 屿§srcï¼å å«å¾åç宿´ç URLï¼å å«å¾åçåºç¡ URLã HTMLImageElement.srcset-
ä¸ä¸ª
String表示 HTML 屿§srcsetï¼å å«äºåéå¾åå表ï¼ç¨éå·åéï¼',', U+002C COMMAï¼ãä¸ä¸ªåéçå¾åæ¯ä¸ä¸ª URL è·çä¸ä¸ª'w'表示å¾åçå®½åº¦ï¼æè ä¸ä¸ª'x'表示åç´ å¯åº¦ã HTMLImageElement.useMap-
ä¸ä¸ª
String表示 HTML 屿§usemapï¼å å«ä¸ä¸ª<map>å ç´ ç页颿¬å° URLãThe page-local URL is a pound (hash) symbol (#) followed by the ID of the<map>element, such as#my-map-element. The<map>in turn contains<area>elements indicating the clickable areas in the image. HTMLImageElement.width-
ä¸ä¸ªæ´æ°ï¼è¡¨ç¤º HTML 屿§
widthï¼è¯´æå¾åå¨ CSS åç´ ä¸æ¸²æç宽度ã HTMLImageElement.xåªè¯»-
An integer indicating the horizontal offset of the left border edge of the image's CSS layout box relative to the origin of the
<html>element's containing block. HTMLImageElement.yåªè¯»-
The integer vertical offset of the top border edge of the image's CSS layout box relative to the origin of the
<html>element's containing block.
å·²åºå¼ç屿§
HTMLImageElement.align-
ä¸ä¸ª
Stringï¼è¡¨ç¤ºå¾åå¦ä½ä¸å®å¨å´çå 容对é½ãThe possible values are"left","right","justify", and"center". This is obsolete; you should instead use CSS (such astext-align, which works with images despite its name) to specify the alignment. HTMLImageElement.border-
ä¸ä¸ª
Stringï¼è¡¨ç¤ºå¾åè¾¹æ¡çå®½åº¦ãæ¤å±æ§å·²è¢«å¼ç¨ï¼åºè¯¥ç¨ CSSborder屿§æ¥ä»£æ¿å®ã HTMLImageElement.hspace-
ä¸ä¸ªæ´æ°å¼ï¼æå®å¾åå·¦å³ççç½ï¼åä½ä¸ºåç´ ã
HTMLImageElement.longDesc-
ä¸ä¸ª
Stringï¼specifying the URL at which a long description of the image's contents may be found. This is used to turn the image into a hyperlink automatically. Modern HTML should instead simply place an<img>inside an<a>element defining the hyperlink. HTMLImageElement.lowSrc-
ä¸ä¸ª
Stringï¼specifying the URL of a low-quality (but faster to load) version of the same image. This was once used by browsers under constrained network conditions or on slow devices. HTMLImageElement.name-
ä¸ä¸ª
Stringï¼representing the name of the element. HTMLImageElement.vspace-
ä¸ä¸ªæ´æ°å¼ï¼æå®å¾åä¸ä¸ççç½ï¼åä½ä¸ºåç´ ã
æ¹æ³
ä»å®çç¶å
ç´ HTMLElement ç»§æ¿çæ¹æ³ã
HTMLImageElement.decode()-
Returns a
Promisethat resolves when the image is decoded and it's safe to append the image to the DOM. This prevents rendering of the next frame from having to pause to decode the image, as would happen if an undecoded image were added to the DOM.
é误
- The
srcattribute is empty ornull. - The specified
srcURL is the same as the URL of the page the user is currently on. - The specified image is corrupted in some way that prevents it from being loaded.
- The specified image's metadata is corrupted in such a way that it's impossible to retrieve its dimensions, and no dimensions were specified in the
<img>element's attributes. - The specified image is in a format not supported by the user agent.
If an error occurs while trying to load or render the image, and an onerror event handler has been configured to handle the error event, that event handler will get called. This can happen in a number of situations, including:
ä¾å
var img1 = new Image(); // Image æé å¨
img1.src = "image1.png";
img1.alt = "alt";
document.body.appendChild(img1);
var img2 = document.createElement("img"); // ä½¿ç¨ DOM HTMLImageElement
img2.src = "image2.jpg";
img2.alt = "alt text";
document.body.appendChild(img2);
// ä½¿ç¨ææ¡£ä¸ç第ä¸ä¸ª img
alert(document.images[0].src);
è§è
| è§è |
|---|
| HTML > # htmlimageelement > |
æµè§å¨å ¼å®¹æ§
åè§
- å®ç°äºè¿ä¸ªæ¥å£ç HTML å
ç´ ï¼
<img>