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

閮借浜嗭紝杩欓噷鏄鎺㈣琛屼负鏄惁鍚堢悊锛岃屼笉鏄彛娴嬪紑鍙戣呮槸涓嶆槸鈥滄柊鎵嬧濄傚鏋滃ぇ澶氭暟绋嬪簭鍛樼殑鐩磋瀵 img.src = null
鏄竻绌哄浘鐗囷紝閭d箞鐜板湪瑙勮寖灏辨槸涓嶅悎鐞嗙殑銆傝繖璺熸槸涓嶆槸鏂版墜娌℃湁鍏崇郴銆

杩欓噷鍜宯ullable涔熸病鏈夊叧绯汇傞棶棰樺湪浜庢槸鍚﹁[TreatNullAs]銆傛垜鐨勬剰瑙侊紝DOM鐨勭粷澶у鏁癉OMString灞炴ч兘搴旇[TreatNullAs=EmptyString]涓旀渶濂絒TreatUndefinedAs=Null]銆傚湪杩欎竴鐐逛笂锛學ebKit鐨勮涓哄ぇ澶氭暟鏄繖鏍风殑銆傛瘮濡
className = null銆

鎵浠ラ棶棰樺嚭鍦ㄦ爣鍑嗕笂銆俉ebIDL鏄愭寮鍙戠殑锛孌OM鏍囧噯鏄垎寮鐨勩傚弽杩囨潵娴忚鍣ㄥ疄鐜板張閫愭闈犳嫝WebIDL銆傚FF绛夈傝繖浜涗笉鍚屾瀵艰嚧nul鐨勯棶棰樻病鏈夊緱鍒版伆褰撶殑瑙e喅銆



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

>  閮借浜嗗晩锛屽枩娆㈢敤null鐨勯兘鏄粠鍏朵粬璇█鍒氳浆杩囨潵鐨勬柊鎵嬶紝浠栦滑杩欐牱鍐欑殑鐩殑灏辨槸瑕佹竻绌簊rc灞炴с
>
> 瀵逛簬src灞炴э紝浣犱篃鐪嬪埌浜嗭紝閭f槸涓狣OMString鐨勶紝鑰屼笉鏄疍OMString?鐨刐1]锛屾墍浠ュ畠蹇呴』鏈変釜鍊笺
> 鍙﹀杩欎釜灞炴у湪Getter閲岃瀹氬叾鍐呭鏄釜path鎴杧path锛岃宲ath鍙垎涓哄叿鏈塻chema鐨勭粷瀵瑰湴鍧鍜屾病鏈塻chema鐨勭浉瀵瑰湴鍧锛
> 绌哄瓧绗︿覆灏辨槸娌℃湁浠讳綍鎸囧悜鐨勭浉瀵瑰湴鍧銆
> 鑰屽疄闄呬笂濡傛灉src娌℃湁鍊肩殑鏃跺欏氨涓嶆樉绀哄浘鐗囥備絾濡傛灉褰撳墠鍦板潃鏄釜鍥剧墖锛岃宨mg.src鏄┖瀛楃涓茬殑鏃跺欙紝js鑳藉緱鍒板浘鐗囧湴鍧锛岃屾祻瑙堝櫒鍗翠笉鏄 绀哄浘鐗囥
>
> 杩欓噷鐭涚浘鐨勫湴鏂瑰湪浜庯細娓叉煋鐨勬椂鍊欏垽鏂簡src鏄惁鏄┖瀛楃涓诧紝鑰実et灞炴х殑鏃跺欏嵈杩涜杩欓」娌″垽鏂
> 杩欐槸HTML鍜孞S锛屼袱涓爣鍑嗕箣闂寸殑涓嶄竴鑷淬
>
>
> [1]:http://dev.w3.org/2006/webapi/WebIDL/#idl-nullable-type

>
> 浜 2013/10/28 0:57, John Hax 鍐欓亾:
>
> 鎷滄墭銆傝繖閲屼笉鏄瑙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澶达紝鑰屽垽瀹氫负鐩稿寮曠敤锛岃岀┖瀛楃涓插湪浜庡綋鍓嶉〉鍦板潃杩涜璺緞 璁$畻鐨勬椂<https:%E7%9A%84schema%E5%A4%B4%EF%BC%8C%E8%80%8C%E5%88%A4%E5%AE%9A%E4%B8%BA%E7%9B%B8%E5%AF%B9%E5%BC%95%E7%94%A8%EF%BC%8C%E8%80%8C%E7%A9%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9C%A8%E4%BA%8E%E5%BD%93%E5%89%8D%E9%A1%B5%E5%9C%B0%E5%9D%80%E8%BF%9B%E8%A1%8C%E8%B7%AF%E5%BE%84%E8%AE%A1%E7%AE%97%E7%9A%84%E6%97%B6>鍊欐病鏈夊仛浠讳綍鏀瑰彉锛屽洜姝よ繑鍥炵綉椤靛湴鍧鏄纭殑锛屼笉鍙戦佽姹備篃鏄纭殑锛屽洜涓鸿繖涓湴鍧鐨勪笢瑗挎鍦ㄦ樉绀恒
>>
>>
>>  鎴戝掓槸娌℃湁娉ㄦ剰鍒版寜鐓х幇鍦ㄧ殑瑙勮寖 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
>>
>>
>
> --
> Regards
>
> Hawkeyes Wind
>
>

Received on Monday, 28 October 2013 03:09:34 UTC