根据以下代码,我编写了一个js方法。在webview的onPageFinished方法中,我去执行它,但是没有达到我想要的效果。媒体的价值没有改变。 这是我的js代码:
mWebView.loadUrl("javascript:(function(){" +
"var links = document.getElementsByTagName(\"link\");" +
"for (var i = 0; i < links.length; i++){" +
"var link = links[i];" +
"if(link.getAttribute(\"href\").indexOf(\"pad.css\") != -1) {" +
"link.setAttribute(\"media\", \"(min-width:9999px)\");" +
"}" +
"if(link.getAttribute(\"href\").indexOf(\"phone.css\") != -1) {" +
"link.setAttribute(\"media\", \"(max-width:9999px)\");" +
"}" +
"}" +
"})()");
我将在onPageFinished方法中执行它。
这是html代码:
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="../META-INF/css/pad.css" media="(min-width:600px)"/>
<link rel="stylesheet" type="text/css" href="../META-INF/css/phone.css" media="(max-width:600px)"/>
<title>info</title>