写到关于UIWebView不加载图片时,查了很多资料,
关于js,关于oc,在stackoverflow 等 并没有找到合适的答案,
最终觉得还是应该回归传统 用js代码,
stringByEvaluatingJavaScriptFromString 来调用js方法
js代码:
var div_hide_css_id = "iBrowser_div_hide_css"; // ------------------- 有图/无图 ---- function JSHandleHideImage () { var i = 0; var allDiv = document.getElementsByTagName("DIV"); for (i; i<allDiv.length; i++) { var e = allDiv[i]; var bg_image = e.style.backgroundImage; e.style.backgroundImage = "none"; e.setAttribute("bg_image", bg_image); } var newCss = document.getElementById(div_hide_css_id); if(newCss == undefined){ document.documentElement.innerHTML= document.documentElement.innerHTML+"<style id='"+div_hide_css_id+"'>img{visibility:hidden;}</style>"; } else { if(newCss.innerHTML == '') { } newCss.innerHTML = 'img{visibility:hidden;}'; } } function JSHandleShowImage () { var i = 0; var allDiv = document.getElementsByTagName("DIV"); for (i; i<allDiv.length; i++) { var e = allDiv[i]; var bg_image = e.getAttribute("bg_image"); e.style.backgroundImage = bg_image; } var newCss = document.getElementById(div_hide_css_id); if(newCss){ newCss.innerHTML = 'img{visibility:visibility;}'; } }
oc代码
if (//无图) { [webView stringByEvaluatingJavaScriptFromString:@"JSHandleHideImage()"]; }else{ [webView stringByEvaluatingJavaScriptFromString:@"JSHandleShowImage()"]; }