Re: img.src = null(原:src 与 srcset(或是 src-N)的 JS 交互)

img.src = '' 鐨勮涓哄湪鐜板湪娴忚鍣ㄩ噷璨屼技鏄細

img.src杩斿洖褰撳墠缃戦〉鐨勭粷瀵瑰湴鍧锛屼絾鏄笉鍙戣姹傦紝褰撶劧涔熸病鏈夊浘鐗囨樉绀恒傚簲璇ヨ鏄瘮杈冭寮傜殑銆



鎴戝掓槸娌℃湁娉ㄦ剰鍒版寜鐓х幇鍦ㄧ殑瑙勮寖 img.src = null 瀹為檯鎴愪簡 img.src = 'null'銆
鎴戣涓鸿鑼冨簲璇ユ敼鍙樿繖涓涓恒傚弽姝e簲璇ュ湪鐜板疄涓病鏈変汉渚濊禆 img.src = null 鐨勮涓恒





2013/10/26 Kang-Hao (Kenny) Lu <kanghaol@oupeng.com>

> (2013/10/25 21:52), John Hax wrote:
> > 1鐨勬柟娉曟槸鎶奿mg.src鏄庣‘涓烘渶缁堟樉绀虹殑鍥剧墖锛屼篃灏辨槸鎺掗櫎浜嗗悓鏃惰缃畇rcset/src
> > 鐨刢ase銆備篃灏辨槸璇村鏋滄墜鍔ㄨ缃簡src锛屽氨瑕嗙洊浜唖rcset鐨勮缃傞櫎闈炰綘鍐
> > img.src = null锛堝綋鍓嶈鑼冩湭瀵硅繖鏍峰簲璇ユ墽琛岀殑琛屼负鍋氬嚭鏄庣‘瑙勫畾锛屾祻瑙堝櫒涓鑸
> > 鏄拷鐣ャ備絾濡傛灉閲囧彇1锛屽垯鍙栨秷瀵箂rcset鐨勮鐩栨槸姣旇緝鍚堢悊鐨勮涓猴紝涓旀琛屼负涓
> > 娴忚鍣ㄥ綋鍓嶈涓烘槸鍏煎鐨勶級銆
>
> 褰撳墠瑙勮寖浠湁瀵 "img.src = null" 瀵硅繖鏍峰簲璇ユ墽琛岀殑琛屼负鍋氬嚭鏄庣‘瑙勫畾锛岃鍙傝
> WebIDL 灞炴ц鍊肩畻娉昜1]涓 ECMAScript 鍊艰浆鎴 IDL DOMString 鍊肩殑绠楁硶[2]銆傝繖
> 閲岀殑鍏抽敭鏄
>
> interface HTMLImageElement : HTMLElement {
>            ...
>            attribute DOMString src;
>            ...
> }
>
> 鐨 attribute 鍓嶉潰鏈夋病鏈 [TreatNullAs=EmptyString] 鏍囩銆傛病鏈夊氨鏄 null 浼
> 琚浆鎴 "null"銆
>
> 褰撶劧锛屾湁鏄庣‘瑙勫畾璺熷悎涓嶅悎瀹炵幇鎴栨槸鍚堜笉鍚堢悊鏄袱鍥炰簨锛宻rc 灞炴ц涓嶈鏈
> [TreatNullAs=EmptyString] 涔熸湁浜夎繃[3]銆傜洰鍓嶆槸
>
>   瑙勮寖 = 鏂 Firefox = IE 鈮 鏃 Firefox = WebKit
>
> 鐨勬牱瀛愩
>
> [1] http://www.w3.org/html/ig/zh/wiki/WebIDL#dfn-attribute-setter

> [2]
> http://dev.w3.org/2006/webapi/WebIDL/#dfn-convert-idl-to-ecmascript-value

> [3] https://www.w3.org/Bugs/Public/show_bug.cgi?id=21668

>
> (2013/10/25 23:39), Kang-Hao (Kenny) Lu wrote:
> >    HTML                       <img src=xxx srcset=xxx />
> > +  涓嶆敮鎸 srcset 鐨 JS 搴 1   璁剧疆浜 .src
> > +  鏀寔 srcset 鐨 JS 搴 2     璁剧疆浜 .srcset
> >
> > 閭h繖涓椂鍊 JS 搴 2 浼氫笉璧蜂綔鐢ㄣ傛敮鎸 .srcset 鐨 JS 搴撶殑瀹屾暣姝g‘鍐欐硶鏄
> >
> >   img.src = null;鈥
> >   img.srcset = 鏂板;
>
> 鎵浠ヨ繖閲岀殑姝g‘鍐欐硶搴旇鏄
>
>   img.src = "";
>   img.srcset = 鏂板;
>
> 銆備笉杩囨垜鎹釜鏂瑰悜闂紝搴旇寰堝浜洪兘鏈夋敞鎰忓埌鍦 DOM 閲 null 澶у浼氬彉鎴
> "null"锛宨mg.src 鐨勭壒鍒箣澶勫湪鍝紵
>
>
> 璇濊锛屾垜鎰熻澶氫簡瑙 WebIDL 瀵瑰緢澶氫汉鐪嬭鑼冮兘寰堟湁甯姪锛屾瘮濡備箣鍓
> [ArrayClass] 鐨勮璁篬4]鎴栬鎴戜滑搴旇浼樺厛缈昏瘧 WebIDL锛
>
> [4]
> http://lists.w3.org/Archives/Public/public-html-ig-zh/2012Jun/thread#msg18

>
> 浠ヤ笂
>
> Kenny
> --
> Web Specialist, Opera Sphinx Team, Oupeng Browser, Beijing
> Try Sphinx: http://sphinx.oupeng.com/

>
>

Received on Sunday, 27 October 2013 13:57:32 UTC