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

鎷滄墭銆傝繖閲屼笉鏄瑙i噴琛屼负鑳屽悗鐨勬妧鏈師鍥狅紙浣犺鐨勯偅浜涙垜鏃╁氨鐭ラ亾锛夛紝鑰屾槸杩欎簺琛屼负鏄惁绗﹀悎寮鍙戣呯殑棰勬湡銆傛瘮濡 img.src = null
锛屽紑鍙戣呰繖鏍峰啓鏄兂杈惧埌浠涔堢洰鐨勶紵
FF鐨勮涓轰篃鏄渶杩戞墠鏀圭殑锛屽師鏉ユ槸鍜寃ebkit涓鏍风殑銆

鑷充簬 img.src = '' 锛屾垜璁や负鐜板湪娴忚鍣ㄧ殑琛屼负鍏跺疄鏄寮傜殑銆傚洜涓哄綋鍓島rl涔熷彲鑳借繑鍥炵湡鐨勫浘鐗囷紙http鍐呭鍗忓晢鍙互鍋氬埌鐨勶級銆
濡傛灉璁╂垜閫夋嫨锛屽簲璇ラ夋嫨 img.src = '' 涔嬪悗 img.src 浠嶆棫杩斿洖绌哄瓧绗︿覆鑰屼笉鏄綋鍓嶇綉椤靛湴鍧銆



2013/10/27 Hawkeyes Wind <hawkeyes0.cn@gmail.com>

>
> 浜 2013/10/27 21:57, John Hax 鍐欓亾:
>
> img.src = '' 鐨勮涓哄湪鐜板湪娴忚鍣ㄩ噷璨屼技鏄細
>
>  img.src杩斿洖褰撳墠缃戦〉鐨勭粷瀵瑰湴鍧锛屼絾鏄笉鍙戣姹傦紝褰撶劧涔熸病鏈夊浘鐗囨樉绀恒傚簲璇ヨ鏄瘮杈冭寮傜殑銆
>
>
>   src涓虹┖瀛楃涓茬殑鏃跺欙紝棣栧厛鑻辨枃娌℃湁http:鎴杊ttps:鐨剆chema澶达紝鑰屽垽瀹氫负鐩稿寮曠敤锛岃岀┖瀛楃涓插湪浜庡綋鍓嶉〉鍦板潃杩涜璺緞璁$畻鐨勬椂鍊欐病鏈夊仛浠讳綍鏀瑰彉锛屽洜姝よ繑鍥炵綉椤靛湴鍧鏄纭殑锛屼笉鍙戦佽姹備篃鏄纭殑锛屽洜涓鸿繖涓湴鍧鐨勪笢瑗挎鍦ㄦ樉绀恒
>
>
>  鎴戝掓槸娌℃湁娉ㄦ剰鍒版寜鐓х幇鍦ㄧ殑瑙勮寖 img.src = null 瀹為檯鎴愪簡 img.src = 'null'銆
> 鎴戣涓鸿鑼冨簲璇ユ敼鍙樿繖涓涓恒傚弽姝e簲璇ュ湪鐜板疄涓病鏈変汉渚濊禆 img.src = null 鐨勮涓恒
>
>
>   杩欎釜琛屼负鍦↗avascript涓篃鏄纭殑锛屽洜涓哄湪js涓湡姝d负绌虹殑鏄痷ndefined锛岃屼笉鏄痭ull锛岀敤if灏卞彲浠ユ槑鏄剧殑鐪嬪嚭鏉ャ俲s涓
> null琛ㄧず涓涓壒娈婄殑瀵硅薄锛岄偅涔堝湪杞崲涓哄瓧绗︿覆鐨勬椂鍊欎細鐩存帴璋冪敤toString()鏂规硶锛宯ull鏈韩鐨勬弿杩板氨鏄"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/

>>
>>
>
> --
> Regards
>
> Hawkeyes Wind
>
>

Received on Sunday, 27 October 2013 16:57:42 UTC