由於幫人解決IE6 png不透明的bug,看到了對方使用jquery的pngfix,問題很快就找到了,是因為類庫衝突,也就是引入了兩個類庫導致的$衝突。
自己之前很少關注這方面東西,出於好奇看了一下源碼,發現實在不堪入目,就突發奇想採用原生的JS重構了一遍。如下:
/** * png fix by yansong at 2011-12-07 * * @example $.ready(function(){YS.pngFix();}); * @desc Fixes all PNG's in the document on document.ready * * @example $.ready(function(){YS.pngFix(document.getElementById('examples'));}); * @desc Fixes all PNG's within div with class examples * */// elem: document.getElementById('#example') || documentYS.pngFix = function(elem) {// IE 6if (document.uniqueID && !window.XMLHttpRequest) {if(elem !== undefined){if(elem.tagName == 'IMG'){fixImg(elem);return;}if(elem.tagName == 'INPUT'){fixInput(elem);return;}fixBg([elem]);}else{elem = document;}var reg1 = new RegExp().compile('url[\(][\'\"]?'),reg2 = new RegExp().compile('[\'\"]?[\)]');// fix imgfunction fixImg(elem){var imgs = elem.getElementsByTagName('img'),len = imgs.length,img,spanP,span,spanStyle,width,height;//console.log(len)while(len--){img = imgs[len];width = img.width || img.currentStyle.width;height = img.height || img.currentStyle.height;//console.log(1);if(img.src.indexOf('.png') !== -1){// parent nodespanP = document.createElement('span');spanStyle = spanP.style;spanStyle.position = 'relative';spanStyle.display = 'inline';spanStyle['float'] = img.currentStyle['float']||'none';spanStyle.border = img.currentStyle.border||'none';spanStyle.padding = img.currentStyle.padding||'0';spanStyle.margin = img.currentStyle.margin||'0';spanStyle.cursor = img.parentNode.currentStyle.cursor;// chidren nodespan = document.createElement('span');spanP.appendChild(span);spanStyle = span.style;spanStyle.cssText = 'position:relative;white-space:pre-line;display:inline;background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + img.src + '\', sizingMethod=\'crop\');';spanStyle.width = width;spanStyle.height = height;span.id = img.id;span.className = img.className;span.title = img.title;span.alt = img.alt;// do changeimg.parentNode.replaceChild(spanP, img);}}}// fix css background pngsfunction fixBg(elems){var len = elems.length, elem, bg;while(len--){elem = elems[len];bg = elem.currentStyle.backgroundImage;//console.log(bg);if(bg.indexOf('.png') != -1){elem.style.backgroundImage = 'none';// url('......') | url("...") | url(...)elem.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg.replace(reg1, '').replace(reg2, '') + "',sizingMethod='crop')";}}}//fix input with png-sourcefunction fixInput(elem){var inputs = elem.getElementsByTagName('input'), len = inputs.length, input, src;while(len--){input = inputs[len];src = input.src;if(src&&src.indexOf('.png')!=-1){input.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + src + '\', sizingMethod=\'crop\');';input.src = 'blank.gif';}}}fixBg(elem.all);fixImg(elem);fixInput(elem);}};
其中yss遭遇到的最大問題就是在擷取background-image時的問題,通過elem.currentStyle.backgroundImage擷取到的值有三種情況:
1. url('http://www.test.com/1.png');
2. url("http://www.test.com/1.png");
3. url(http://www.test.com/1.png);
所以需要通過Regex去匹配並刪除,來擷取到:http://www.test.com/1.png