JavaScriptÓëImage¼ÓÔØʼþ(onload)¡¢¼ÓÔØ״̬(complete)
ÒÔǰд¹ýÒ»¸öͼƬµÈ±ÈËõ·ÅµÄJsº¯Êý£¬È±ÏÝÊÇÒªµÈµ½ËùÓÐͼƬ¶¼¼ÓÔØÍê±ÏÁË£¬²ÅÄܽøÐеȱÈËõ·Å¡£
×òÌìÓÃjQuery²å¼þaeImageResize£¬·¢ÏÖËü¸üÓÐÓÅÊÆ£ºÃ¿ÕÅͼƬ¼ÓÔØÍêºó£¬»áÂíÉϽøÐеȱÈËõ·Å¡£
Õâ¹éÓÚͼƬ¶ÔÏóImageµÄ¼ÓÔØʼþonloadµÄ¹¦ÀÍ¡£
²é¿´²å¼þµÄÔ´Â룬·¢ÏÖËüÒ²ÒÀÀµÍ¼Æ¬¶ÔÏóµÄcompleteÊôÐÔºÍonloadʼþ£¬²¢ÇÒÌرð°ÑIE6Çø·Ö¶Ô´ý£¬µ½µ×IE6ÔÚͼƬ¼ÓÔضÔÏóÉÏ£¬ÓëÆäËüä¯ÀÀÆ÷ÓÐʲô²»Í¬ÄØ£¿
¿´ÏÂÎÄ£º
====================================================================
ͨ¹ýjs²Ù×ÝDOMºÜ¶àÇé¿ö϶¼ÊÇΪÁËʵÏֺ͵±Ç°Ò³htmlÔªËصÄÒì²½ÔØÈ룬ÎÒ̸̸¶ÔImage¶ÔÏóµÄһЩÈÏʶ¡£
¿´¸öÀý×Ó£º
<input type="button" name="" value="ÔØÈëͼƬ" onclick="addImg('tt.jpg')" />
<script type="text/javascript">
<!--
function addImg(isrc)
{
var Img = new Image();
Img.src = isrc;
Img.onload = function ()
{
document.body.appendChild(Img);
}
}
//-->
</script>
µ±°üº¬ÉÏÊö´úÂëµÄÒ³Ãæ´ò¿ªÊ±²¢²»ÔØÈë¡°tt.jpg¡±£¬µ±µã»÷°´Å¥Ê±ºò²ÅÔØÈë¡£µ±ÔØÈëÍê³Éºó´¥·¢onloadʼþÏÔʾµ½Ò³ÃæÉÏ¡£Èç¹ûÄãÊǵÚÒ»´Î¼ÓÔØ ¡°tt.jpg" ÕâÕÅͼƬµÄ»°£¬ÔËÐÐÕý³£¡£µã»÷°´Å¥¼ÓÔز¢ÏÔʾһÕÅͼƬ£¬Èç¹ûÖظ´µã»÷»áÔõôÑùÄØ£¿
IE¡¢OperaÖУ¬³ýÁ˵ÚÒ»´Î¼ÓÔØ Í¼Æ¬Ê±ºòÏÔʾÕý³££¬Ö®ºóÔÙµã»÷¾ÍûÓз´Ó¦ÁË£¬Ë¢ÐÂÒ²Ò»Ñù¡£ÄѵÀËüÃÇÖ»´¥·¢Ò»´Î ¡°onload¡±Ê¼þ£¿ÊÇ»º´æ»úÖÆ£¿
FF¡¢ChromÖУ¬Ã¿µã»÷Ò»´Î¼ÓÔØÒ»ÕŸÃͼƬ¡£
ÉÔ΢ÐÞ¸ÄÏ£º
<input type="button" name="" value="ÔØÈëͼƬ" onclick="addImg('tt.jpg')" />
<script type="text/javascript">
<!--
function addImg(isrc)
{
var Img = new Image();
Img.onload = function ()
{
document.body.appendChild(Img);
}
Img.src = isrc;
}
//-->
</script>
ÔËÐкó·¢ÏÖ£¬Ææ¹ÖµÄÊÂÇé·¢ÉúÁË¡£
ËùÓеÄä¯ÀÀÆ÷¶¼Ò»ÖÂÁË£¬¶¼ÊÇÿµã»÷Ò»´Î¼ÓÔØÒ»ÕÅͼƬ¡£ÕâÓÖÊÇʲôÔÒò£¿ÓÉ´Ë¿ÉÒÔ¼û IE¡¢Opera Ö´Ðйý³ÌÖв¢²»ÊÇÖ»´¥·¢Ò»´Î onload ʼþ£¡
ÁªÏëһϠImage ¶ÔÏóµÄһЩÊôÐÔ¿´¿´£¬complete¡¢readyState(IEרÊôÖµ[uninitialized,complete]) £¨Îª·ÀÖ¹»º´æÓ°ÏìЧ¹ûÇë¸ü»»Í¼Æ¬Ãû³Æ£¡£©
<input type="button" name="" value="complete" onclick='alert("complete : "+Img.complete +"\\nreadyState : "+Img.readyState)' /> <input type="button" name="" value="ÔØÈëͼƬ" onclick="addImg('mtm.jpg')" /> <script type="text/javascript"> <!-- var Img; function addImg(isrc) { Img = new Image(); //Img.src = isrc; Img.onload = function () { alert("complete : "+Img.complete +"\nreadyState : "+Img.readyState) document.body.appendChild(Img); } Img.src = isrc; } //--> </script>
¾¹ýÒÔÉϲâÊÔ£¬¿ÉÒÔ¿´³öһЩ²»Í¬µã£¬¶ÔÓÚ complete ÊôÐÔÀ´½²£¬IEÊǸù¾ÝͼƬÊÇ·ñÏÔʾ¹ýÀ´Åжϣ¬¾ÍÊÇ˵µ±¼ÓÔصÄͼƬÏÔʾ³öÀ´ºó£¬complete ÊôÐÔµÄÖµ²ÅΪ true £¬·ñÔòÒ»Ö±ÊÇ false £¬ºÍÒÔÇ°ÊÇ·ñ¼ÓÔعý¸ÃÕÅͼƬûÓйØϵ£¬¼´ºÍ»º´æûÓйØϵ£¡µ«ÊÇÆäËüä¯ÀÀÆ÷±íÏÖ³öÀ´µÄÈ·²»Ò»Ñù£¬Ö»ÒªÒÔÇ°¼ÓÔعý¸Ãͼ£¬ä¯ÀÀÆ÷Óлº´æ£¬complete ¾ÍΪ true £¬ÕâºÍIEµÄ readyState ÊôÐԵıíÏÖÒ»Ö£¡
ÖÁ´Ë£¬¿ÉÒԿ϶¨µÄÊÇËùÓеÄä¯ÀÀÆ÷¶¼»á»º´æͼƬ£¡¿ÉÊÇÉÏÃæµÄÎÊÌâµ½µ×ÊÇʲôÔÒòµ¼ÖµÄÄØ£¿
ÖÚËùÖÜÖª£¬´Ó»º´æÀï¼ÓÔض«Î÷µÄËÙ¶ÈÊǺܿìµÄ£¬ÄÇôÔÚ
... Img.src = isrc; Img.onload = ... ...µÄ¹ý³ÌÖУ¬ÄѵÀ IE¡¢Opera ¼ÓÔصÄËٶȿ쵽£¬À´²»¼°×·¼Óʼþ£¿
Õâ»Ø¼ÓÔØÒ»ÕŸù±¾²»´æÔÚµÄͼƬ¿´¿´Ð§¹û£º
<input type="button" name="" value="complete" onclick='alert("complete : "+Imgttmt.complete +"\\nreadyState : "+Imgttmt.readyState)' /> <input type="button" name="" value="ÔØÈëͼƬ" onclick="addImg('mtmttyt.jpg')" /> <script type="text/javascript"> <!-- var Imgttmt; function addImg(isrc) { Imgttmt = new Image(); Imgttmt.src = isrc; alert("complete : "+Imgttmt.complete +"\nreadyState : "+Imgttmt.readyState) Imgttmt.onload = function () { alert("impossible") } } //--> </script>
¿ÉÒԿ϶¨µÄÊÇËùÓÐä¯ÀÀÆ÷¶¼²»´¥·¢ onload ʼþ¡£
´ÓÊÇ·ñ»º´æ»òÒѾ¼ÓÔعýͼƬµÄ½Ç¶È½²£¬IE¡¢Opera±íÏÖÕý³££¬complete ʼÖÕΪ false £»IEµÄ readyState ʼÖÕΪuninitialized ¡£
ÁîÈËÒÉ»óµÄÊÇFF£¬ÆäÖÐ Imgttmt.complete µÄÖµÒ»Ö±ÊÇ true £»
¸üÁîÈËÀ§»óµÄÊÇ Chrom£¬ËüÊÇÔÚ×î³õ new Imgttmt() µÄʱºò Imgttmt.complete ֵΪ false¡£¶øÖ®ºó Imgttmt.complete Öµ¾ÍһֱΪ true ÁË£¡
Èç¹û»»Ò»ÕÅ´ÓÀ´Ã»ÓмÓÔعýµÄͼƬ£¬FFºÍChrom µÄÐÐΪ¾ÍÒ»ÖÂÁË£¬¶¼ÊÇÒ»¿ªÊ¼¼ÓÔØʱ£¬ Imgttmt.complete ֵΪfalse£¬ Ö®ºóΪ true£¡
²âÊԵĹý³ÌÖл¹·¢ÏÖ£¬½Å±¾µÄÖ´ÐÐ˳ÐòµÄÈ·»áÓ°Ïìµ½ÀàËÆÓÚ onload µÈʼþµÄ×·¼Ó£¬Èç¹ûÔÚÆäÏÔʾºóÔÚ×·¼Óʼþ¾ÍûÓÐʲôʵ¼ÊÒâÒåÁË£¡
»ùÓÚ javascript ÕâÖÖ½âÊÍÐÔÓïÑÔµÄÌØÐÔ£¬ÔÚ×·¼ÓʼþµÄʱºòÒ»¶¨Òª×¢Òâ°Ñʼþ×·¼ÓÔÚ´¥·¢¸ÃʼþµÄ¾ä±ú֮ǰ¡£
×ÊÁÏÀ´Ô´£ºhttp://hi.baidu.com/mataofq/blog/item/5675ee24da5d4d6935a80f66.html
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ